NSUserDefaults:Attempt to insert non-property value Objective C

 问题:

Attempt to set a non-property-list object{...}as an NSUserDefaults value for key shop.

解决办法:

if you don't want to bother about ensuring all your dictionary values are the property list types, then you can simply convert the dictionary into NSData,

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:[NSKeyedArchiver archivedDataWithRootObject:self.myDictionary] forKey:@"MyData"];
[def synchronize];

And to get back into a dictionary from sandbox:

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSData *data = [def objectForKey:@"MyData"];
NSDictionary *retrievedDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
self.myDictionary = [[NSDictionary alloc] initWithDictionary:retrievedDictionary];

Hope this helps someone. 

posted on 2017-09-01 10:21  雨季的雾  阅读(341)  评论(0编辑  收藏  举报