1.什么是 歸檔 和 解檔
數(shù)據(jù)本地存儲持久化的一種芥喇。
歸檔:對象的序列化权逗,通過某種格式把對象保存成本地文件病往。
解檔:反序列化捣染,把歸檔的對象文件讀成原來的對象。
2.具體過程
- 在歸檔對象的
.h
中遵循規(guī)定協(xié)議<NSCoding>
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Person : NSObject<NSCoding>
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *age;
@property (nonatomic,copy)NSString *number;
@end
NS_ASSUME_NONNULL_END
- 在歸檔對象的
.m
中實現(xiàn)協(xié)議方法
#import "Person.h"
@implementation Person
- (NSString *)description{
return [NSString stringWithFormat:@"%@\n%@\n%@",_name,_age,_number];
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
// 告訴系統(tǒng)歸檔的屬性是哪些
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.age forKey:@"age"];
[aCoder encodeObject:self.number forKey:@"number"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
// 解檔
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeObjectForKey:@"age"];
self.number = [aDecoder decodeObjectForKey:@"number"];
}
return self;
}
@end
- 在
vc
中進(jìn)行存取操作(以temp
路徑為例)
(1)歸檔過程
Person *p = [[Person alloc] init];
p.name = @"wxh";
p.age = @"18歲";
p.number = @"23號";
NSString *filePath = NSTemporaryDirectory();
NSString *uniquePath = [filePath stringByAppendingPathComponent:@"p_wxh.plist"];
[NSKeyedArchiver archiveRootObject:p toFile:uniquePath];
(2)解檔過程
NSString *filePath = NSTemporaryDirectory();
NSString *uniquePath = [filePath stringByAppendingPathComponent:@"p_wxh.plist"];
Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:uniquePath];
NSLog(@"%@",p);
(3)刪除文件
- (void)deleteFileWithFileName:(NSString *)fileName filePath:(NSString *)filePath {
//創(chuàng)建文件管理對象
NSFileManager* fileManager = [NSFileManager defaultManager];
//獲取文件目錄
if (!filePath) {
//如果文件目錄設(shè)置有空,默認(rèn)刪除Cache目錄下的文件
filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
}
//拼接文件名
NSString *uniquePath=[filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
//文件是否存在
BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
//進(jìn)行邏輯判斷
if (!blHave) {
NSLog(@"文件不存在");
return ;
}
else {
// 文件是否被刪除
BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
// 進(jìn)行邏輯判斷
if (blDele) {
NSLog(@"刪除成功");
}
else {
NSLog(@"刪除失敗");
}
}
}
(4)獲取文件路徑有三種方法
// 獲取沙盒Document路徑
filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
// 獲取沙盒temp路徑
filePath = NSTemporaryDirectory();
// 獲取沙盒Cache路徑
filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
// 文件路徑
NSString *uniquePath = [filePath stringByAppendingPathComponent:你的文件名稱];
3.使用runtime
當(dāng)在歸檔對象.m
中實現(xiàn)協(xié)議方法時停巷,如果對象的屬性比較多耍攘,這個時候就會很繁瑣榕栏。那么可以利用runtime
獲取屬性,簡化過程蕾各。
#import "Person.h"
#import <objc/runtime.h>
@implementation Person
- (NSString *)description{
unsigned int count;
Ivar *ivar = class_copyIvarList([self class], &count);
NSString *descString = @"打印:";
for (int i = 0; i<count; i++) {
Ivar iva = ivar[i];
const char *name = ivar_getName(iva);
NSString *strName = [NSString stringWithUTF8String:name];
//利用KVC取值
id value = [self valueForKey:strName];
descString = [NSString stringWithFormat:@"%@\n%@",descString,value];
}
free(ivar);
return descString;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
unsigned int count;
Ivar *ivar = class_copyIvarList([self class], &count);
for (int i = 0; i<count; i++) {
Ivar iva = ivar[i];
const char *name = ivar_getName(iva);
NSString *strName = [NSString stringWithUTF8String:name];
//利用KVC取值
id value = [self valueForKey:strName];
[aCoder encodeObject:value forKey:strName];
}
free(ivar);
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
unsigned int count = 0;
Ivar *ivar = class_copyIvarList([self class], &count);
for (int i = 0; i < count; i++) {
Ivar iva = ivar[i];
const char *name = ivar_getName(iva);
NSString *strName = [NSString stringWithUTF8String:name];
//進(jìn)行解檔取值
id value = [aDecoder decodeObjectForKey:strName];
//利用KVC對屬性賦值
[self setValue:value forKey:strName];
}
free(ivar);
}
return self;
}
@end