Sqlite 存储自定义对象

在iOS中如果想保存自定义对象,要让自定义对象实现NSCoding接口并实现方法-(id)initWithCoder:(NSCoder *)coder-(void)encodeWithCoder:(NSCoder *)coder;

@interface RegUserInfo : NSObject <NSCoding>   
@property (nonatomic, strong) NSString *siteName;
@end  
@@implementation RegUserInfo
-(void) encodeWithCoder:(NSCoder *)encoder  {  
    [encoder encodeObject:siteName forKey:@"siteName"];  
} 
-(id) initWithCoder:(NSCoder *)decoder {  
    siteName = [decoder decodeObjectForKey:@"siteName"];    
    return self;  
}  
//两个方法中的key必须一致
@end

如果项目中使用了mjextention,可以直接在.m文件中调用MJCodingImplementation


@interface MarkerUniformConfig : NSObject<NSCoding>//需要继承NSCoding
@property(nonatomic,strong)NSString* icon_id;
@end


@implementation MarkerUniformConfig
MJCodingImplementation//这里会自动完成属性序列化过程
@end
posted @ 2016-07-28 21:00  coolwxb  阅读(789)  评论(0编辑  收藏  举报