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