标签:obj 使用 int data sort return dealloc save archive
项目里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
标签:obj 使用 int data sort return dealloc save archive
原文地址:http://www.cnblogs.com/yutingliuyl/p/6815871.html