如果Plistdocuments directory
不存在,则将其复制到。
如果它已经存在,我想使用 in 中的“Name NSDictionary
”bundleArray
键NSDictionary
在documentsArray
.
找到匹配项后,我想检查字符串中的更改并在有更改时替换它们。
如果未找到匹配项,则意味着必须将此字典添加到文档 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'
我怎样才能解决这个问题?
解决此问题后,您认为我的代码会起作用吗?
如果没有,我会很高兴就如何做到这一点提出一些建议。我已经挣扎了一段时间,真的需要发布带有更正的更新!非常感谢你的帮助。