这是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
}
}
提前致谢。
答案 0 :(得分:4)
myWordsPath
可能不包含任何有效文件,并且您正在从该路径中存在的文件的内容初始化NSMutableDictionary
。这就是你得到一个空字典的原因。
答案 1 :(得分:0)
首先,尝试检查myWordsPath
,如果它返回null,您应该查找并找到正确的路径。
其次,你如何生成你的plist? MyWords.plist
必须包含有效格式。如果你不确定,你应该在XCode中创建,有plist对象。
答案 2 :(得分:0)
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:
@"MyWords" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc]
initWithContentsOfFile:plistPath];
答案 3 :(得分:0)
请尝试此代码..
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
documentPath = [documentPath stringByAppendingPathComponent:@"MyWords.plist"];
NSMutableDictionary *myWordsDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:documentPath];