这是我的UIAlertView委托方法。我不知道为什么在运行应用程序时myWordsDictionary为null。 (注意:_dailyWords是一个NSDictionary对象,而带有书签的是一个NSString对象。)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex]) {
NSLog(@"Clicked button index !=0");
// Add the action here
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *myWordsPath = [documentPath stringByAppendingPathComponent:@"MyWords.plist"];
NSMutableDictionary *myWordsDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:myWordsPath];
[myWordsDictionary setValue:[_dailyWords objectForKey:bookmarked] forKey:bookmarked];
[myWordsDictionary writeToFile:myWordsPath atomically:YES];
NSLog(@"%@", [myWordsDictionary description]);
[myWordsDictionary release];
} else {
NSLog(@"Clicked button index = 0");
// Add another action here
}
}
提前致谢。
最佳答案
myWordsPath
可能不包含任何有效文件,并且您正在根据该路径中存在的文件的内容来初始化NSMutableDictionary
。这就是为什么您得到空字典的原因。