0

UIAlertView のデリゲート メソッドを次に示します。アプリの実行時に myWordsDictionary が null である理由がわかりません。(注: _dailyWords は NSDictionary オブジェクトで、bookmarked は 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
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:
                       @"MyWords" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc]
                            initWithContentsOfFile:plistPath];
于 2012-12-21T07:00:48.277 に答える
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

まず、 check を試してみてくださいmyWordsPath。null が返された場合は、正しいパスを探して見つける必要があります。

第二に、どのように plist を生成しますか? MyWords.plist有効な形式が含まれている必要があります。不明な場合は、XCode 内から作成する必要があります。plist オブジェクトがあります。

于 2012-12-21T06:57:32.100 に答える