我不能在NsUserDefaults中存储NSArray.该应用程序崩溃
在这一行
[savedData setObject:jsonValue forKey:@“ JSONDATA”];
在这一行
[savedData setObject:jsonValue forKey:@“ JSONDATA”];
NSArray *jsonValue =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSUserDefaults *savedData = [NSUserDefaults standardUserDefaults]; [savedData setObject:jsonValue forKey:@"JSONDATA"]; [savedData synchronize];
错误日志:
*** Terminating app due to uncaught exception '`NSInvalidArgumentException`',reason: '*** - [NSUserDefaults setObject:forKey:]: attempt to insert non-property list object <CFBasicHash 0x8c62b10 [0x1d2aec8]>{type = immutable dict,count = 3,entries =>
解决方法
是的,它们不能像NSUserDefaults那样保存.
我正在写一个代码,请看看,更多的学习去看苹果docs好的.
代码在这里:
//保存
NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:yourArrayToBeSave]]; [[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:@"array"]; [[NSUserDefaults standardUserDefaults] synchronize]; // this will save you UserDefaults
//用于检索
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"array"]; NSArray *savedArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];