新しいNSJSONSerializationクラスとNSPropertyListSerializationクラスを使用してJSONファイルをplistに変換しています。私は自分のJSONをPlistにエラーなく変換することができますが、私の最後のステップで、plistを私のデスクトップに書き込むと、プログラムはクラッシュしますが、Plistが生成された後です!NSData writeToFileはPlistを正常に書き込みますが、NSInvalidArgumentExceptionを返してクラッシュします。
NSData *data = [[NSData alloc] initWithContentsOfURL:path]; \\(NSURL *)path -->goes to my JSON file
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
//the following removes all key/object pairs where the object is null, because NSPropertyListSerialization with throw an error if there are null values
for (id __strong object in [json objectForKey:@"terms"]) {
if ([object objectForKey:@"image"] == [NSNull null]) {
[object removeObjectForKey:@"image"];
}
}
//the following NSPropertyListSerialization method returns an NSData
id plist = [NSPropertyListSerialization dataFromPropertyList:(id)json
format:NSPropertyListXMLFormat_v1_0
errorDescription:nil];
NSError *writeToFileError;
[plist writeToFile:@"/Users/kalaracey/Desktop/test.plist"
atomically:YES
encoding:NSUTF8StringEncoding
error:&writeToFileError];
次に、この最後の行では、NSInvalidArgumentException
がスローされ、私のプログラムがクラッシュしています。しかし、plistは正常に生成されました!私はそれを読むことができ、私のプログラムのクラッシュ以外はすべてうまくいっています。
誰かがこのクラッシュの原因を説明して、クラッシュを回避する方法を教えてください。
例外: –
例外は、最後のメソッドのための 'NSInvalidArgumentException'です。つまり、plist(と思われる)が' writeToFile:atomicallyencoding:error: 'に応答しません。 – kalaracey