我正在研究 iPhone 开发并面临读取/写入 plist 文件的问题。我遵循了 iPhone 开发书中的示例,但在运行时不断收到错误消息。
错误消息说:2012-04-26 00:21:09.759 FileHandling[5915:207] -[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x685ac40
这是示例代码(对我来说似乎很好......不过):
NSString *plistFileName = [[self documentPath] stringByAppendingPathComponent: @"Apps.plist"];
NSLog(@"Where is the file? => %@", plistFileName);
if ([[NSFileManager defaultManager] fileExistsAtPath:plistFileName]) {
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistFileName];
for (NSString *category in dict) {
NSLog(@"%@", category);
NSLog(@"=========");
NSArray *titles = [dict valueForKey:category];
for (NSString *title in titles) {
NSLog(@"%@", title);
}
}
} else {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Apps" ofType: @"plist"];
NSLog(@"%@", plistPath);
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile: plistPath];
NSLog(@"Let's take a look : %@", dict);
NSMutableDictionary *copyOfDict = [dict mutableCopy];
NSLog(@"Let's look at the mutable dictationary : %@", copyOfDict);
NSArray *categoriesArray = [[copyOfDict allKeys] sortedArrayUsingSelector: @selector(compare:)];
for (NSString *cateogry in categoriesArray) {
NSArray *titles = [dict valueForKey: cateogry];
NSMutableArray *mutableTitles = [titles mutableCopy];
[mutableTitles addObject: @"New App Title"];
[copyOfDict setObject: mutableTitles forKey:cateogry];
}
NSString *fileName = [[self documentPath] stringByAppendingPathComponent: @"Apps.plist"];
[copyOfDict writeToFile: fileName atomically:YES];
}