我不能在NsUserDefaults中存储NSArray.该应用程序崩溃
在这一行
[savedData setObject:jsonValue forKey:@“ JSONDATA”]; @H_502_4@以下是我的代码我在下面提到我的日志错误
在这一行
[savedData setObject:jsonValue forKey:@“ JSONDATA”]; @H_502_4@以下是我的代码我在下面提到我的日志错误
NSArray *jsonValue =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSUserDefaults *savedData = [NSUserDefaults standardUserDefaults]; [savedData setObject:jsonValue forKey:@"JSONDATA"]; [savedData synchronize];@H_502_4@错误日志:
*** 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那样保存.
@H_502_4@我正在写一个代码,请看看,更多的学习去看苹果docs好的.
@H_502_4@代码在这里:
@H_502_4@//保存
NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:yourArrayToBeSave]]; [[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:@"array"]; [[NSUserDefaults standardUserDefaults] synchronize]; // this will save you UserDefaults@H_502_4@//用于检索
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"array"]; NSArray *savedArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];