0

这是我的 UIAlertView 委托方法。我不知道为什么 myWordsDictionary 在运行应用程序时为空。(注意:_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
    }
}

提前致谢。

4

4 回答 4

4

myWordsPath可能不包含任何有效文件,并且您正在NSMutableDictionary从该路径中存在的文件的内容初始化您的文件。这就是为什么你得到一个空字典。

于 2012-12-21T06:16:43.780 回答
0

请尝试此代码..

NSString *documentPath =    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES)  objectAtIndex:0];
documentPath =    [documentPath stringByAppendingPathComponent:@"MyWords.plist"]; 
NSMutableDictionary *myWordsDictionary = [[NSMutableDictionary alloc]    initWithContentsOfFile:documentPath];
于 2012-12-21T07:33:35.447 回答
0

首先,尝试检查myWordsPath,如果返回 null,您应该查找并找到正确的路径。

其次,你如何生成你的 plist?MyWords.plist必须包含有效格式。如果你不确定,你应该从 XCode 中创建,有 plist 对象。

于 2012-12-21T06:57:32.100 回答
0
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:
                       @"MyWords" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc]
                            initWithContentsOfFile:plistPath];
于 2012-12-21T07:00:48.277 回答