0

Documents ディレクトリに .plist ファイルが存在するかどうかを確認したいと考えています。存在しない場合は、作成して最初のエントリでシードしたいと思います。存在する場合は、それ読み取り、エントリを追加します。

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {       
    // read Faves file...
    NSMutableDictionary *tempDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
    appDelegate.dictFaves = tempDict;
} else {
    NSLog(@"Creating Favorites file");
    BOOL result = [[NSFileManager defaultManager] createFileAtPath: path contents: (NSData *)appDelegate.dictFaves attributes: nil];            
}

// .... and Append it to the list of existing favorites
[appDelegate.dictFaves setObject:newFave forKey:key];
  1. createFileAtPathFALSEファイルが作成されなかったことを意味します。
  2. appDelegate.dictFavesへのキャストの有効性に疑問があり(NSDATA *)ます。それが賢明でない場合、? を含むファイルを作成するにはどうすればよいDictionaryですか?
4

1 に答える 1

0

次を使用して記述できます。

-[NSDictionary writeToURL:atomically:]

したがって、次のように言えます。

[appDelegate.dictFaves writeToURL:url atomically:YES];

appDelegate.dictFaves(が有効であると仮定NSDictionary)

于 2012-02-17T09:21:47.577 に答える