Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 用字典纠正plist_Objective C_Ios_Xcode - Fatal编程技术网

Objective c 用字典纠正plist

Objective c 用字典纠正plist,objective-c,ios,xcode,Objective C,Ios,Xcode,如果Plist不存在,则复制到文档目录 如果它已经存在,我想使用bundleray中NSDictionary中的“Name”键在documentsArray中查找匹配的NSDictionary 找到匹配项后,我希望检查字符串中的更改,如果有更改,则替换它们 如果未找到匹配项,则表示必须将此词典添加到文档plist中 这是我的代码: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSD

如果Plist不存在,则复制到
文档目录

如果它已经存在,我想使用
bundleray
NSDictionary
中的“Name”键在
documentsArray
中查找匹配的
NSDictionary

找到匹配项后,我希望检查字符串中的更改,如果有更改,则替换它们

如果未找到匹配项,则表示必须将此词典添加到文档plist中

这是我的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self managePlist];
return YES;
}

- (void)managePlist {

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Objects.plist"];
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Objects" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) {
    [fileManager copyItemAtPath:bundle toPath:path error:&error];

} else {

    NSArray *bundleArray = [[NSArray alloc] initWithContentsOfFile:bundle];
    NSMutableArray *documentArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

    BOOL updateDictionary = NO;
    for(int i=0;i<bundleArray.count;i++) {

        NSDictionary *bundleDict=[bundleArray objectAtIndex:i];

        BOOL matchInDocuments = NO;
        for(int ii=0;ii<documentArray.count;ii++)
        {
            NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii]; 
            NSString *bundleObjectName = [bundleDict valueForKey:@"Name"];
            NSString *documentsObjectName = [documentDict valueForKey:@"Name"];
            NSRange range = [documentsObjectName rangeOfString:bundleObjectName options:NSCaseInsensitiveSearch];
            if (range.location != NSNotFound) {
                matchInDocuments = YES;
            }
            if (matchInDocuments) {

                if ([bundleDict objectForKey:@"District"] != [documentDict objectForKey:@"District"]) {
                    [documentDict setObject:[bundleDict objectForKey:@"District"] forKey:@"District"];
                    updateDictionary=YES;
                }
            }

            else {
                [documentArray addObject:bundleDict];
                updateDictionary=YES;
            }
        }
    }
    if(updateDictionary){
        [documentArray writeToFile:path atomically:YES];
    }

}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项
{
[自我管理主义];
返回YES;
}
-(无效)管理层列表{
n错误*错误;
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*path=[DocumentsDirectoryStringByAppendingPathComponent:@“Objects.plist”];
NSString*bundle=[[NSBundle mainBundle]pathForResource:@“对象”类型:@“plist”];
NSFileManager*fileManager=[NSFileManager defaultManager];
如果(![fileManager fileExistsAtPath:path]){
[fileManager copyItemAtPath:bundle-toPath:path错误:&错误];
}否则{
NSArray*bundlerray=[[NSArray alloc]initWithContentsOfFile:bundle];
NSMutableArray*documentArray=[[NSMutableArray alloc]initWithContentsOfFile:path];
BOOL updateDictionary=否;

对于(int i=0;i来说,
documentDict
可能实际上是不可变的。即使将其分配给
NSMutableDictionary
,这也不能准确描述底层数据

NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii];
应该是:

NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithDictionary:[documentArray objectAtIndex:ii]];
无论您在哪里编辑
文档dict
,请添加:

 [documentArray replaceObjectAtIndex:ii withObject:documentDict];

当您从文件中读取plist时,将创建一个不可变的字典/数组,其中包含不可变的子项,因此

  NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii]; 

line实际上是一个谎言-您没有可变字典。要从plist创建可变对象,请查看并指定
KCFPropertyListMutableContainers和Leaves
作为可变选项。

这是我使用James Webster的答案编写的最终有效代码。感谢H2CO3的贡献

- (void)managePlist {
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Object.plist"];
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Object" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path]) {
    [fileManager copyItemAtPath:bundle toPath:path error:&error];
} 
else 
{
    NSArray *bundleArray = [[NSArray alloc] initWithContentsOfFile:bundle];
    NSMutableArray *documentArray = [[NSMutableArray alloc] initWithContentsOfFile:path];

    BOOL updateDictionary = NO;
    for(int i=0;i<bundleArray.count;i++) {
        NSDictionary *bundleDict=[bundleArray objectAtIndex:i];            

        for(int ii=0;ii<documentArray.count;ii++)
        {
            NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithDictionary:[documentArray objectAtIndex:ii]];
            NSString *bundleObjectName = [bundleDict valueForKey:@"Name"];
            NSString *documentsObjectName = [documentDict valueForKey:@"Name"];
            NSRange range = [documentsObjectName rangeOfString:bundleObjectName options:NSCaseInsensitiveSearch];
            if (range.location != NSNotFound) {
                 NSString *districtString = [bundleDict objectForKey:@"District"];
                if ([documentDict objectForKey:@"District"] != districtString) {
                    [documentDict setObject:districtString forKey:@"District"];
                    [documentArray replaceObjectAtIndex:ii withObject:documentDict];
                    updateDictionary=YES;
                }
            }
        }
    }
    if(updateDictionary){
        [documentArray writeToFile:path atomically:YES];
    }
}
}
-(void)managePlist{
n错误*错误;
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*path=[DocumentsDirectoryStringByAppendingPathComponent:@“Object.plist”];
NSString*bundle=[[NSBundle mainBundle]pathForResource:@“对象”类型:@“plist”];
NSFileManager*fileManager=[NSFileManager defaultManager];
如果(![fileManager fileExistsAtPath:path]){
[fileManager copyItemAtPath:bundle-toPath:path错误:&错误];
} 
其他的
{
NSArray*bundlerray=[[NSArray alloc]initWithContentsOfFile:bundle];
NSMutableArray*documentArray=[[NSMutableArray alloc]initWithContentsOfFile:path];
BOOL updateDictionary=否;

对于(int i=0;i它不会更新属性列表,因为当变得可变时会复制字典-OP需要我的解决方案。编辑应该解决这个问题,而不必将CoreFoundation与NextStep混合使用。谢谢,这是一个非常简单的解决方案,它完美地完成了任务