归档(NSCoding)--->持久化存储
作为持久化存储的一种, NSKeyedArchiver - 对象归档
1.首先要先继承NSCoding协议.
2.实现归档和解归档的两个方法如下:
/**编码的方法,将数据编码成可以写入本地的状态*/ - (void)encodeWithCoder:(NSCoder *)aCoder; /**解码方法,从本地读取数据,重新创建对象进行初始化*/ - (instancetype)initWithCoder:(NSCoder *)aDecoder;
3.存储:(可以从服务器上直接获取一个字典存储,也可以存储到沙盒中读取).
1 2 3 4 | /**从服务器上获取*/ + (User *)getUserLoginSuccessInfoDic:(NSDictionary *)infoDic; /**存储到沙盒中*/ NSKeyedUnarchiver 从二进制流读取对象。<br>[NSKeyedArchiver archiveRootObject:stu toFile:path]; 存储 |
4.读取
1 | 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; }
将来的自己,会感谢现在不放弃的自己!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现