當(dāng)使用字典或者數(shù)組寫入 writeToFile:@"xx/xx.plist" atomically:true]; 返回值為NO 一般有兩個(gè)原因:
- 1.要寫入的文件夾 沒(méi)有寫入權(quán)限.
- 2.集合內(nèi)不能存儲(chǔ)自定義對(duì)象,也不能是null.
If an array or dictionary contains objects that are not property-list objects, then you cannot save and restore the hierarchy of data using the various property-list methods and functions.
解決方式:
簡(jiǎn)單的序列化一下就可以了:比如
NSData *data = [NSJSONSerialization dataWithJSONObject:message options:0 error:NULL];
[data writeToFile:@"xx/xx.json" atomically:true];
這樣字典或者數(shù)組里有空值 也可以存儲(chǔ)
如果字典里有自定義對(duì)象的話 可以使用歸檔:
//obj是字典或者數(shù)組
[NSKeyedArchiver archiveRootObject:obj toFile:@"xx/xx.archive"];
但是一定要為你的自定義對(duì)象類遵守<NSCoding> 并且實(shí)現(xiàn):
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;