我不能在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那样保存.
//保存
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];