归档(NSCoding)--->持久化存储
作为持久化存储的一种, NSKeyedArchiver - 对象归档
1.首先要先继承NSCoding协议.
2.实现归档和解归档的两个方法如下:
/**编码的方法,将数据编码成可以写入本地的状态*/ - (void)encodeWithCoder:(NSCoder *)aCoder; /**解码方法,从本地读取数据,重新创建对象进行初始化*/ - (instancetype)initWithCoder:(NSCoder *)aDecoder;
3.存储:(可以从服务器上直接获取一个字典存储,也可以存储到沙盒中读取).
/**从服务器上获取*/ + (User *)getUserLoginSuccessInfoDic:(NSDictionary *)infoDic; /**存储到沙盒中*/ NSKeyedUnarchiver 从二进制流读取对象。
[NSKeyedArchiver archiveRootObject:stu toFile:path]; 存储
4.读取
NSKeyedUnarchiver unarchiveObjectWithFile:path
5.具体例子:
5.1沙盒
5.11 存储
// 1.新的模型对象 User *user = [[User alloc] init]; user.name = @"星仔"; user.age = 18; user.height = 1.85; // 2.归档模型对象 // 2.1.获得Documents的全路径 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // 2.2.获得文件的全路径 NSString *path = [doc stringByAppendingPathComponent:@"user.data"]; // 2.3.将对象归档 [NSKeyedArchiver archiveRootObject:stu toFile:path];
5.12 读取
// 1.获得Documents的全路径 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // 2.获得文件的全路径 NSString *path = [doc stringByAppendingPathComponent:@"user.data"]; // 3.从文件中读取MJStudent对象 MJStudent *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
5.13实现归档与解档方法
/** * 将某个对象写入文件时会调用 * 在这个方法中说清楚哪些属性需要存储 */ - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:self.name forKey:@"name"]; [encoder encodeInt:self.age forKey:@"age"]; [encoder encodeDouble:self.height forKey:@"height"]; } /** * 从文件中解析对象时会调用 * 在这个方法中说清楚哪些属性需要存储 */ - (id)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { // 读取文件的内容 self.no = [decoder decodeObjectForKey:@"name"]; self.age = [decoder decodeIntForKey:@"age"]; self.height = [decoder decodeDoubleForKey:@"height"]; } return self; }
5.2从服务器上获取
/**从服务器上获取*/ + (User *)getUserLoginSuccessInfoDic:(NSDictionary *)infoDic { if (infoDic == nil ||(NSNull *)infoDic == [NSNull null]) return nil; User *loginUser = [[User alloc] init]; loginUser.UUID = [infoDic objectForKey:@"UUID"]; NSDictionary *userInfoDic = [infoDic objectForKey:@"user"]; if (userInfoDic) { loginUser.ID = [[userInfoDic objectForKey:@"id"] integerValue]; loginUser.contactsname = [userInfoDic objectForKey:@"contactsname"]; loginUser.contactsmail = [userInfoDic objectForKey:@"contactsmail"]; loginUser.contactsphone = [userInfoDic objectForKey:@"contactsphone"]; loginUser.headportrait = [userInfoDic objectForKey:@"headportrait"]; }else{ loginUser.contactsname = @""; loginUser.contactsmail = @""; loginUser.contactsphone = @""; } return loginUser; }
5.21.实现归档和解归档方法
/**归档*/ - (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.UUID forKey:@"l_uuid"]; [aCoder encodeInteger:self.ID forKey:@"l_id"]; [aCoder encodeObject:self.contactsname forKey:@"l_contactsname"]; [aCoder encodeObject:self.contactsmail forKey:@"l_contactsmail"]; [aCoder encodeObject:self.contactsphone forKey:@"l_contactsphone"]; [aCoder encodeObject:self.headportrait forKey:@"l_headportrait"]; } /**解档*/ - (instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { self.UUID = [aDecoder decodeObjectForKey:@"l_uuid"]; self.ID = [aDecoder decodeIntegerForKey:@"l_id"]; self.contactsname = [aDecoder decodeObjectForKey:@"l_contactsname"]; self.contactsmail = [aDecoder decodeObjectForKey:@"l_contactsmail"]; self.contactsphone = [aDecoder decodeObjectForKey:@"l_contactsphone"]; self.headportrait = [aDecoder decodeObjectForKey:@"l_headportrait"]; } return self; }
将来的自己,会感谢现在不放弃的自己!