2012年2月11日土曜日

[iOS]writeToFileでfalseが返る

NSDictionaryでwriteToFileでfalseが返る

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
path = [path stringByAppendingPathComponent:@"config.plist"];
NSMutableDictionary *configDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];
[configDict setObject:newDict forKey:@"newDict"];
if ([configDict writeToFile:path2 atomically:YES]) {
     TRACE("OK");
} else {
     TRACE("NG");
}

writeToFileでエラーが"NG"が返るのでずっと悩んでたんだが
理由はこれ
This method recursively validates that all the contained objects are property list objects (instances of NSDataNSDateNSNumberNSString,NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

このメソッドの再帰的に含まれているすべてのオブジェクトは、ファイルを書き出す前に、プロパティリストオブジェクト(NSDataの、NSDate、NSNumber、NSString、NSArray、またはNSDictionaryのインスタンス)であることを検証しておらず、すべてのオブジェクトがプロパティリストオブジェクトでない場合はNOを返すので結果ファイルが有効なプロパティのリストではありません。

objectがおかしいよってことで
[configDict setObject:newDict forKey:@"newDict"];
のnewDictにNSDictionaryっぽくないデータが入っていた
plistはこんな感じなる
key = value

newDictの中身はこうなっていた
"key" = value
JSONからの変換時に""が付与されてwriteToFileができなかったというというオチ

0 件のコメント:

コメントを投稿