最近项目中有一个发帖子的需求,就是类似新浪微博中的发文章。现在要求增加草稿箱的功能,不同的帖子都可以将内容存储到本地。开始我打算用数据库做本地存储,但是,由于文本编辑有富文本内容,对应iOS中的NSAttributedString对象,这种对象存储到数据库没有对应的数据类型,然后就采用了做本地序列化和反序列化来存储内容,也就是归解档。
发帖页面大概是这样的,支持添加标题、封面、文本和图片,文本中支持插入话题和联系人:
利用runtime来做序列化,主要是省了自己去一个一个解析类的成员变量和属性了 ,在增加新的属性的时候,可以不用再去关心属性的解析了。注意:如果在子类中实现序列化和反序列化,需要考虑父类属性。
直接贴代码了:
@interface SendPostDraftsBoxModel : NSObject
@property(nonatomic, copy)NSString *postId;//帖子ID
@property(nonatomic, copy)NSAttributedString *title;//标题
@property(nonatomic, strong)UIImage *coverImage;//封面
@property(nonatomic, strong)NSArray *contentArray;//拼装的内容,存的是字典,字典里边存的NSAttributeString和UIImage
//序列化到本地
+ (BOOL)writeDraftsBoxModelWithModel:(SendPostDraftsBoxModel *)model;
//反序列化,返回model对象
+ (SendPostDraftsBoxModel *)readDraftsBoxModelWithPostId:(nonnull NSString *)postId;
//删除本地的草稿箱model对象
+ (BOOL)deleteDraftsBoxModelWithPostId:(nonnull NSString *)postId;
@end
@implementation SendPostDraftsBoxModel
+ (BOOL)supportsSecureCoding {
return YES; //支持加密编码
}
- (void)encodeWithCoder:(NSCoder *)coder {
unsigned int count;
Ivar *varArray = class_copyIvarList([self class], &count);
for (int i = 0; i < count; i++) {
Ivar var = varArray[i];
const char *cName = ivar_getName(var);
NSString *proName = [NSString stringWithUTF8String:cName];
//去掉成员变量前边的"_"
if (proName.length > 0 && [[proName substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"_"]) {
proName = [proName substringFromIndex:1];
}
id value = [self valueForKey:proName];
[coder encodeObject:value forKey:proName];
}
free(varArray);
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super init];
if (self) {
unsigned int count;
Ivar *varArray = class_copyIvarList([self class], &count);
for (int i = 0; i < count; i++) {
Ivar var = varArray[i];
const char *cName = ivar_getName(var);
NSString *proName = [NSString stringWithUTF8String:cName];
//去掉成员变量前边的"_"
if (proName.length > 0 && [[proName substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"_"]) {
proName = [proName substringFromIndex:1];
}
id value = [coder decodeObjectForKey:proName];
if (value != nil) {
[self setValue:value forKey:proName];
}
}
free(varArray);
}
return self;
}
+ (BOOL)writeDraftsBoxModelWithModel:(SendPostDraftsBoxModel *)model {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = [NSString stringWithFormat:@"/SendPostDraftsBoxModel_%@.achive", model.postId ? model.postId : @""];
NSString *docPath = [path stringByAppendingString:fileName];
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:docPath];
if (!isExist) {
[[NSFileManager defaultManager] createFileAtPath:docPath contents:nil attributes:nil];
}
return [NSKeyedArchiver archiveRootObject:model toFile:docPath];
}
+ (SendPostDraftsBoxModel *)readDraftsBoxModelWithPostId:(nonnull NSString *)postId {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = [NSString stringWithFormat:@"/SendPostDraftsBoxModel_%@.achive", postId ? postId : @""];
NSString *docPath = [path stringByAppendingString:fileName];
SendPostDraftsBoxModel *model = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
return model;
}
+ (BOOL)deleteDraftsBoxModelWithPostId:(nonnull NSString *)postId {
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *fileName = [NSString stringWithFormat:@"/SendPostDraftsBoxModel_%@.achive", postId ? postId : @""];
NSString *docPath = [path stringByAppendingString:fileName];
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:docPath];
if (isExist) {
return [[NSFileManager defaultManager] removeItemAtPath:docPath error:nil];
}
return YES;
}
@end
这里有一个考虑父类的情况,并且进行了封装,大家也可以参考:https://github.com/weng1250/WZLSerializeKit