-1

如果Plistdocuments directory不存在,则将其复制到。

如果它已经存在,我想使用 in 中的“Name NSDictionarybundleArrayNSDictionarydocumentsArray.

找到匹配项后,我想检查字符串中的更改并在有更改时替换它们。

如果未找到匹配项,则意味着必须将此字典添加到文档 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];
    }

}

}

如果我现在运行我的应用程序,我会收到以下消息:'-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

我怎样才能解决这个问题?

解决此问题后,您认为我的代码会起作用吗?

如果没有,我会很高兴就如何做到这一点提出一些建议。我已经挣扎了一段时间,真的需要发布带有更正的更新!非常感谢你的帮助。

4

3 回答 3

3

大概documentDict是实际上是不可变的。即使您将其分配给NSMutableDictionary,这也不能准确地描述基础数据。

NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii];

应该:

NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithDictionary:[documentArray objectAtIndex:ii]];

无论您在何处编辑documentDict,添加:

 [documentArray replaceObjectAtIndex:ii withObject:documentDict];
于 2012-09-16T21:07:44.760 回答
0

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

  NSMutableDictionary *documentDict = [documentArray objectAtIndex:ii]; 

line 完全是一个谎言——你没有可变字典。要从 plist 创建可变对象,请查看CFPropertyListCreateWithData并指定kCFPropertyListMutableContainersAndLeaves为可变性选项。

于 2012-09-16T21:11:01.277 回答
0

这是我使用 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];
    }
}
}
于 2012-09-17T18:39:12.783 回答