TAG

RSS订阅

收藏本站

设为首页

当前位置:主页 > 移动开发 > IOS开发 >

使用 NSUserDefaults 读取和写入自定义对象(Attempt to set a no

发布时间:2015-01-06 17:34 类别:IOS开发

众所周知,NSUserDefaults只能保存诸如NSArray、NSDictionary、NSData、NSNumber等基本数据类型,如果我们强制保存自定义的类,就会出现这个错误:Attempt to set a non-property-list object as an NSUserDefaults value for ,解释起来:【试图将一个非属性列表对象设置为 NSUserDefaults】接下来就说说如何吧自定义的对象保存到NSUserDefaults中去。
自定义的类实现<NSCoding>协议中的- (id) initWithCoder: (NSCoder *)coder方法和- (void) encodeWithCoder: (NSCoder *)coder方法
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
#pragma mark NSCoding  
- (id)initWithCoder:(NSCoder *)aDecoder{  
  
    if (self == [super init]) {  
        alias = [aDecoder decodeObjectForKey:JSON_NAME];  
        mobile = [aDecoder decodeObjectForKey:JSON_MOBILE];  
        signtime = [[aDecoder decodeObjectForKey:JSON_TIMESTAMP] longValue];  
        endtime = [[aDecoder decodeObjectForKey:JSON_END_TIME] longValue];  
        cmobile = [aDecoder decodeObjectForKey:JSON_CMOBILE];  
    }  
    return self;  
}  
  
- (void)encodeWithCoder:(NSCoder *)aCoder{  
  
    [aCoder encodeObject:alias forKey:JSON_NAME];  
    [aCoder encodeObject:mobile forKey:JSON_MOBILE];  
    [aCoder encodeObject:[NSNumber numberWithLong:signtime] forKey:JSON_TIMESTAMP];  
    [aCoder encodeObject:[NSNumber numberWithLong:endtime] forKey:JSON_END_TIME];  
    [aCoder encodeObject:cmobile forKey:JSON_CMOBILE];  
  
}  
 
保存到NSUSerDefault:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
Terminal *terminal = [[Terminal alloc] init];  
  
terminal.alias = [dict objectForKey:JSON_NAME];  
terminal.mobile = [dict objectForKey:JSON_MOBILE];  
terminal.signtime = [[dict objectForKey:JSON_TIMESTAMP] longValue];  
terminal.endtime = [[dict objectForKey:JSON_END_TIME] longValue];  
terminal.cmobile = [dict objectForKey:JSON_CMOBILE];  
  
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:terminal];  
  
[userDefaults setObject:data forKey:"test"];  
  
[userDefaults synchronize];  
也就是说,我们保存自定义对象时,是使用NSKeyedArchiver 把数据归档为NSData对象,然后把NSData存储到UserDefault中,NSData相当于Model
读取:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];  
  
NSData *data =  [userDefaults objectForKey:"test"];  
  
return [NSKeyedUnarchiver unarchiveObjectWithData:data];  
 读取自定义对象时,先获取到NSData,然后使用NSKeyedUnarchiver解档为自定义的对象
LOG输出,查看结果:
2014-12-10 16:31:11.815 ESO_Etws[1463:60b] alias:Q611-0334
2014-12-10 16:31:11.815 ESO_Etws[1463:60b] mobile:13841040334
2014-12-10 16:31:11.815 ESO_Etws[1463:60b] signtime:1394529151000
2014-12-10 16:31:11.816 ESO_Etws[1463:60b] endtime:1426065151000
2014-12-10 16:31:11.816 ESO_Etws[1463:60b] cmobile: 
 
PS:
APP升级后,UserDefaults中原有的plist是不会删除的,除非用户卸载APP
清除整个UserDefaults数据的方法:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];  
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];  
猜你会喜欢....

Copyright © 2015 www.wahenzan.com 哇!很赞 版权所有 浙ICP备14030256号-1 Power by DedeCms

浙公网安备 33010602005986号

声明:本站所有文章除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除 网站管理员:758763728

360网站安全检测平台