2012-04-25 13 views
0

私はiPhone開発を勉強しており、読み書きplistファイルの問題に直面しています。私はiPhone開発の本の例に従ったが、実行中にエラーメッセージが表示され続ける。iPhone iOS 5の読み書きplier

エラーメッセージは言う: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]; 
} 

答えて

1

エラーメッセージによると、問題が__NSCFDictionaryaddObject:への呼び出しで発生しています。これは、実行時に辞書がオブジェクトを追加するメッセージを受け取ったことを意味します。

ただし、このコードスニペットでは、addObject:は、明らかにNSMutableArrayに送信されています。これはおそらく、最終的なfor-loopのdictから取得している各オブジェクトtitlesが配列ではなく、実際にはコードが配列として参照している別の辞書であることを意味します。

実際、コードは整形式であると思われるので、ソースplistの整形式を確認してください。それをプレーンテキストエディタで開きます。また、トンのロギングをロギングするので、この方法で確認します。出力では、辞書(ルートエントリを含む)は{curly = braces}と表示され、配列は(round parentheses)と表示されます。

関連する問題