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

本文共 3708 字,大约阅读时间需要 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

你可能感兴趣的文章
Android后台服务拍照的解决方式
查看>>
SQL Server索引
查看>>
VC UTF8转ANSI
查看>>
企业应用开发模式 ERP项目中应用到的技术和工具
查看>>
Java:多线程,Exchanger同步器
查看>>
计算字符串和文件的MD5值
查看>>
Visual Studio 项目中添加include, lib, dll库文件(*.h,*.lib,*.dll)
查看>>
查询sql server 表结构
查看>>
php操作xml
查看>>
(C#)Windows Shell 外壳编程系列8 - 同后缀名不同图标?
查看>>
poj 2923(状态压缩+背包)
查看>>
Bootstrap3.0学习第十九轮(JavaScript插件——标签页)
查看>>
android 无法生成R文件的原因剖析
查看>>
Android:WebView
查看>>
Ping批量函数
查看>>
ios 向sqlite数据库插入和读取图片数据
查看>>
Ad Muncher 目前半价优惠^_^
查看>>
[转载]将archlinux 2013-06-01版,安装配置为个人工作站
查看>>
IOS ----UIButton用法详解
查看>>
nutch 大量网站
查看>>