java,mac,object c都有各自的序列化保存方法。
现在介绍下iOS的数据序列化保存。
一般分为用单例来实现数据的序列化保存和用对象来实现数据的序列化保存。
第一类:用单例来实现数据的序列化保存。
用户登录数据的序列化保存一般使用单例来实现,根据是否有登录信息来决定是到登录页面还是到其它页面。
它通常包含获取用户信息,得到用户信息(读取文件),清理用户信息,保存用户信息,用户相关字段,根据服务器返回的数据字典初始化单例数据。
头文件:GlobalSingle.h
import
import
import
import “GlobalSingle.h”
@interface GlobalSingle() @end @implementation GlobalShare static GlobalSingle *singleUserData = nil; (GlobalSingle*)getGlobalSingle { if (singleUserData == nil) { singleUserData = [[super allocWithZone:NULL] init]; } return singleUserData; } (id)allocWithZone:(NSZone *)zone { @synchronized(self) { if (singleUserData == nil) { singleUserData = [super allocWithZone:zone]; return singleUserData; } } return nil; } //为确保 每次返回的都是同一块地址 (id)copyWithZone:(NSZone *)zone { return self; } //清理用户信息 (void)clearUserInfo{ NSString *path = [self PathWithFileName:@”user.archiver”];//拓展名可以自己随便取 User *user = nil; [NSKeyedArchiver archiveRootObject:user toFile:path]; _user = nil; } //保存 -(void)saveinfo:(User *)user { if(nil != user) { NSString *path = [self PathWithFileName:@”user.archiver”];//拓展名可以自己随便取 [NSKeyedArchiver archiveRootObject:user toFile:path]; } } //得到用户信息 -(User *)getUserinfo; { FLDDLogDebug(@”函数”); NSString *path = [self PathWithFileName:@”taxiuser.archiver”];//拓展名可以自己随便取 User *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; return user; } // 返回文件路径 (NSString )PathWithFileName:(NSString )fileName { NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString *path = [documents stringByAppendingPathComponent:fileName]; return path; } @end @implementation User @synthesize sex; -(id)initWithDictionary:(NSDictionary*)dic { if (self=[super init]) { self.name = [NSString stringWithFormat:@”%@”,dic[@”name”] ==NULL?@”“:dic[@”name”]]; } return self; } -(void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.name forKey:@”name”]; } -(id)initWithCoder:(NSCoder *)aDecoder { if (self=[super init]) { self.name= [aDecoder decodeObjectForKey:@”name”]; } return self; } @end
第二类:用对象来实现数据的序列化保存。
上次查询到的消息列表,订单列表等数据,当没有网络时需要显示上次查询的老数据。这些消息列表数据和订单列表数据需要用对象数据的序列化保存。
它通常只需要对需要保存数据model类继承一个子类专门对这些数据进行序列化保存就可以了。
OrderCellModel.h文件
import
import “OrderCellModel.h”
@implementation OrderCellModel + (instancetype)parseDataWithDictionary:(NSDictionary *)dict; { return [[self alloc] initWithDictionary:dict]; } - (instancetype)initWithDictionary:(NSDictionary *)dict { if (self = [super init]) { _orderSNo = dict[@”orderSNo”] == NULL ? @”” : [NSString stringWithFormat:@”%@”,dict[@”orderSNo”]]; } return self; } @end OrderOperateModel.h文件
import
import “OrderCellModel.h”
@interface OrderOperateModel : OrderCellModel {
}
-(id)initWithDictionary:(OrderCellModel*)model;
OrderOperateModel.m文件
import “OrderOperateModel.h”
@implementation OrderOperateModel -(id)initWithDictionary:(OrderCellModel*)model { if (self=[super init]) { self.orderSNo = [[NSString alloc] initWithFormat:@”%@”,model.orderSNo]; } return self; } //将对象编码(即:序列化) -(void) encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.orderSNo forKey:@”orderSNo”]; } //将对象解码(反序列化) -(id) initWithCoder:(NSCoder *)aDecoder { if (self=[super init]) { self.orderSNo = [aDecoder decodeObjectForKey:@”orderSNo”]; } return (self); } @end 调用示例: OrderOperateModel *orderOperateModel = nil; NSMutableArray *arr = [NSMutableArray array]; for(NSUInteger i = 0; i < dataArr.count; i++) { OrderCellModel *model = dataArr[i]; if(![model isKindOfClass:[OrderCellModel class]]) { return; } orderOperateModel = [[OrderOperateModel alloc] initWithDictionary:model]; [arr addObject:orderOperateModel]; } if(arr.count > 0) { NSData * tempArchive = [NSKeyedArchiver archivedDataWithRootObject:arr]; [tempArchive writeToFile:filePath atomically:YES]; }