我正在學習iPhone開發,並面臨讀/寫plist文件的問題。我遵循iPhone開發手冊中的一個例子,但在運行時不斷收到錯誤消息。在iPhone上閱讀/寫作plist iOS 5
該錯誤消息表示:2012-04-26 00:21:09.759 FileHandling [5915:207] - [__ NSCFDictionary ADDOBJECT:]:無法識別的選擇發送到實例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];
}