Sqlite 存储自定义对象

前端之家收集整理的这篇文章主要介绍了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
原文链接:https://www.f2er.com/sqlite/198798.html

猜你在找的Sqlite相关文章