归档用于存储模型
#define DCAccountFilePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"account.data"]
'DCAccountFilePath为存储地址'
1.'存储
+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;
//示例
[NSKeyedArchiver archiveRootObject:account toFile:DCAccountFilePath];
2.'解析
+ (nullable id)unarchiveObjectWithFile:(NSString *)path;
//示例
DCAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:DCAccountFilePath];
3.'要存储的模型必须遵守NSCoding协议,并完成协议中的方法
@interface DCEmotion : NSObject<NSCoding>
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder;
//存储
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.access_token forKey:@"access_token"];
[aCoder encodeObject:self.expires_in forKey:@"expires_in"];
[aCoder encodeObject:self.expires_end forKey:@"expires_end"];
[aCoder encodeObject:self.uid forKey:@"uid"];
}
//读取
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if(self)
{
self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
self.expires_in = [aDecoder decodeObjectForKey:@"expires_in"];
self.expires_end = [aDecoder decodeObjectForKey:@"expires_end"];
self.uid = [aDecoder decodeObjectForKey:@"uid"];
}
return self;
}
本地存储方式-归档
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、简单说明 plist:在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路...
- 本地存储数据方式1.plist文件2.归档3.NSUserDefults4.数据库5.文件操作 数据的持久化存储程...
- ** 前言:**归档是iOS开发中数据存储常用的技巧,归档可以直接将对象储存成文件,把文件读取成对象。相对于pli...