[User Defaults] Attempt to set a non-property-list object () as an NSUserDefaults/CFPreferences valu
2017-02-06 14:48
876 查看
开发时使用userDefaults存储数据运行时崩溃,崩溃日志如下所示:
查阅资料发现NSUserDefaults本身不支持自定义对象的存储,但它支持NSData类型,所以在这里尝试把对象装换成NSData类型存储。如下所示:
直接这样修改运行时崩溃,错误日志如下所示:
发现将AllAppsModel类型变成NSData类型就必须实现归档,在AllAppsModel.h文件中遵守NSCoding协议,在AllAppsModel.m文件中实现encodeWithCoder和initWithCoder 方法。如下所示:
这样处理后所有问题就都解决了。
[User Defaults] Attempt to set a non-property-list object ( ( "<AllAppsModel: 0x17403cce0>", "<AllAppsModel: 0x17403d480>", . . . ), ( "<AllAppsModel: 0x17403cce0>", "<AllAppsModel: 0x17403d480>", . . . ) ) as an NSUserDefaults/CFPreferences value for key editColumn
查阅资料发现NSUserDefaults本身不支持自定义对象的存储,但它支持NSData类型,所以在这里尝试把对象装换成NSData类型存储。如下所示:
1.存储时直接把最外层数组转成NSData类型: NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr]; 2.获取时把data转成数组类型: NSMutableArray *userDefaultsArr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
直接这样修改运行时崩溃,错误日志如下所示:
*** -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
发现将AllAppsModel类型变成NSData类型就必须实现归档,在AllAppsModel.h文件中遵守NSCoding协议,在AllAppsModel.m文件中实现encodeWithCoder和initWithCoder 方法。如下所示:
@interface AllAppsModel : NSObject<NSCoding> //标题 @property (nonatomic, copy)NSString *title; //图片 @property (nonatomic, copy)NSString *picture; @end
#import "AllAppsModel.h" @implementation AllAppsModel - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.title forKey:@"title"]; [aCoder encodeObject:self.picture forKey:@"picture"]; } - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { self.title = [aDecoder decodeObjectForKey:@"title"]; self.picture = [aDecoder decodeObjectForKey:@"picture"]; } return self; } @end
这样处理后所有问题就都解决了。
相关文章推荐
- Attempt to set a non-property-list object null as an NSUserDefaults
- iOS使用NSUserDefaults 存储数据报Attempt to set a non-property-list object as an NSUserDefaults value的错误解决
- Attempt to set a non-property-list object as an NSUserDefaults
- [User Defaults] Attempt to set a non-property-list object
- [User Defaults] Attempt to set a non-property-list object
- NSUserDefaults数据保存报错:Attempt to set a non-property-list object...
- iOS NSUserDefaults数据保存报错:Attempt to set a non-property-list object...
- 错误:*** -[NSUserDefaults setObject:forKey:]: attempt to insert non-property list object
- NSUserDefaults:Attempt to insert non-property value Objective C
- Attempt to set a non-property-list object
- NSUserDefaults:Attempt to insert non-property value Objective C
- Attempt to set a non-property-list object
- Attempt to set a non-property-list object
- *** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value ‘<Student: 0x8399f70>’
- NSUserDefaults 存数据 Attempt to insert non-property list object
- Attempt to set a non-property-list object 报错问题
- chrome javascript Uncaught SecurityError: An attempt was made to break through the security policy of the user agent
- Failed to set () user defined inspected property on (UIButton): [<UIButton 0x7fe5586efeb0> setValue:
- tomcat警告解决办法:WARNING: An attempt was made to authenticate the locked user
- 关于an attempt was made to authenticate the locked user 的问题的想法