NSUserDefaultsカスタムオブジェクトの保存

4241 ワード

プロジェクトでjsonが返すモデルを保存する必要があります.このモデルは固定されていてテーブルを作成する必要はありません.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