博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NSUserDefaults 保存自己定义对象
阅读量:6676 次
发布时间:2019-06-25

本文共 3763 字,大约阅读时间需要 12 分钟。

项目里json返回的一个model须要保存下来,这个model是固定的没必须去创建表,想到了NSUserDefaults来存储,暂不考虑安全问题。

NSUserDefaults没法直接存储一个对象。在网上找到了方法。如今分享下。

首先要写一个对象,详细代码例如以下:

.h文件

@interface CustomSerModel : NSObject@property(nonatomic,retain)NSString *m_deptId;//部门编号@property(nonatomic,retain)NSString *m_sort_key;//姓名首字母,用于分组排序@property(nonatomic,retain)NSString *m_imgUrl;//头像地址@property(nonatomic,retain)NSString *m_imgKey;//头像文件的key@property(nonatomic,retain)NSString *m_signature;//个性签名@property(nonatomic,retain)NSString *m_name;//好友姓名@property(nonatomic,retain)NSString *m_entUserId;//好友企业用户编号@property(nonatomic,retain)NSString *m_entId;//好友企业编号@property(nonatomic,retain)NSString *m_address;//地址@property(nonatomic,retain)NSString *m_mobilePh;//手机号码@property(nonatomic,retain)NSString *m_sex;//性别@property(nonatomic,retain)NSString *m_shortPh;//集团短号- (void)encodeWithCoder:(NSCoder *)aCoder;- (id)initWithCoder:(NSCoder *)aDecoder;+(BOOL) ParserJson:(NSString*)json withArray:(NSMutableArray *)array;@end

.m文件

@implementation CustomSerModel@synthesize m_deptId,m_sort_key,m_imgUrl,m_imgKey,m_signature,m_name,m_entUserId,m_entId,m_address,m_mobilePh,m_sex,m_shortPh;-(void)dealloc{    [m_deptId release];    [m_sort_key release];    [m_imgUrl release];    [m_imgKey release];    [m_signature release];    [m_name release];    [m_entUserId release];    [m_entId release];    [m_address release];    [m_mobilePh release];    [m_sex release];    [m_shortPh release];    [super dealloc];}- (void) encodeWithCoder: (NSCoder *)coder{    [coder encodeObject:self.m_deptId forKey:@"DeptId"];    [coder encodeObject:self.m_sort_key forKey:@"ImgKey"];    [coder encodeObject:self.m_imgUrl forKey:@"ImgUrl"];    [coder encodeObject:self.m_signature forKey:@"Signature"];    [coder encodeObject:self.m_name forKey:@"Name"];    [coder encodeObject:self.m_entId forKey:@"EntId"];    [coder encodeObject:self.m_entUserId forKey:@"EntUserId"];    [coder encodeObject:self.m_address forKey:@"Address"];    [coder encodeObject:self.m_mobilePh forKey:@"MobilePh"];    [coder encodeObject:self.m_sex forKey:@"Sex"];    [coder encodeObject:self.m_shortPh forKey:@"ShortPh"];}- (id) initWithCoder: (NSCoder *) coder{    m_deptId = [[coder decodeObjectForKey:@"DeptId"]copy];    m_imgKey = [[coder decodeObjectForKey:@"ImgKey"]copy];    m_imgUrl = [[coder decodeObjectForKey:@"ImgUrl"]copy];    m_signature = [[coder decodeObjectForKey:@"Signature"]copy];    m_name = [[coder decodeObjectForKey:@"Name"]copy];    m_entId = [[coder decodeObjectForKey:@"EntId"]copy];    m_entUserId = [[coder decodeObjectForKey:@"EntUserId"]copy];    m_address = [[coder decodeObjectForKey:@"Address"]copy];    m_mobilePh = [[coder decodeObjectForKey:@"MobilePh"]copy];    m_sex = [[coder decodeObjectForKey:@"Sex"]copy];    m_shortPh = [[coder decodeObjectForKey:@"ShortPh"]copy];        return self;}+(BOOL) ParserJson:(NSString*)json withArray:(NSMutableArray *)array{    //此处省略    return TRUE;}@end
定义好对象后如今就是怎么使用了,保存方法:

NSMutableArray *arr = [[NSMutableArray alloc]init];            [CustomSerModel ParserJson:json withArray:arr];            NSData *encodemenulist = [NSKeyedArchiver archivedDataWithRootObject:arr];            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];            [defaults setObject:encodemenulist forKey:@"CustomList"];
获取方法:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];    NSData *saveMenulistDaate = [defaults objectForKey:@"CustomList"];    NSMutableArray *arr = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:saveMenulistDaate];
完毕,感谢汗清提供的帮忙。

參考:http://zani.iteye.com/blog/1431239

   http://blog.csdn.net/jianjianyuer/article/details/10112759

 http://blog.sina.com.cn/s/blog_61e26bcb010115cm.html

转载于:https://www.cnblogs.com/yutingliuyl/p/6815871.html

你可能感兴趣的文章
iOS:原生二维码扫描
查看>>
JavaScript数据类型(转)
查看>>
Makefile: (实验) 目标命令的结束标志
查看>>
Python自动化开发学习的第十周----Redis
查看>>
volley7--NetworkDispatcher从网络中获取数据
查看>>
git和GItHub的区别
查看>>
ftp服务器搭建流程详解
查看>>
C#面向对象(四):其他面向对象知识
查看>>
bzoj2539
查看>>
My thoughts after NOIP 2018(1)
查看>>
烂泥:学习Nagios(二):Nagios配置
查看>>
模拟实现 百度翻译 右下方的可折叠的分享按钮列表
查看>>
天降祥瑞,搬家来到cnblogs
查看>>
360 2015校园招聘 第一题
查看>>
Git上传代码的步骤
查看>>
sass基础常用指南
查看>>
数学 Codeforces Round #282 (Div. 2) B. Modular Equations
查看>>
select2清除选择(选择框内的值)
查看>>
超链接的学习与运用
查看>>
create-react-app按需引入antd-mobile
查看>>