首先說明歸解檔是一種編碼方式,不是數(shù)據(jù)本地化的方式,在復雜對象寫入本地的時候?qū)嶋H上使用的還是writeToFile 的簡單寫入本地的方法,且WtiteToFile 直接寫入本地,是整存整取的 在一個路徑下存數(shù)據(jù),最后一次存進去的東西會覆蓋掉之前的,下面我們通過上述兩內(nèi)容實現(xiàn)復雜對象的寫入.
本文內(nèi)容:
一.對Foundation框架中的對象進行歸檔
二.對自定義的對象進行歸檔
三.對自定義內(nèi)容進行歸檔
注意: 歸檔生成的文件是加密文件(是不可解)
首先創(chuàng)建一個person類
Person.h文件
@interface Person : NSObject<NScoding>
//如果一個對象想實現(xiàn)直接寫入本地,那么這個對象就要遵守<NSCoding>協(xié)議
//聲明屬性
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *sex;
@property(nonatomic,assign)NSUInteger age;
@end```
###Person.m文件
```code
#import "Person.h"
@implementation Person
//在歸檔和解檔的時候,要把所有的屬性都進行歸解檔
//歸檔
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.sex forKey:@"sex"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
//解檔
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.sex = [aDecoder decodeObjectForKey:@"sex"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
@end```
然后在視圖控制器ViewController中操作
###ViewController.m 文件中
```code
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//如果一個對象想直接寫入本地,那么這個對象就要遵守NSCoding協(xié)議,上面已經(jīng)說過,一定記住哦
//獲取要存儲數(shù)據(jù)的主路經(jīng)(具體參數(shù)前面有解釋)
NSString *documentPathStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//創(chuàng)建Person對象,做存儲的數(shù)據(jù)
Person *person = [[Person alloc] init];
person.name = @"班長";
person.sex = @"女";
person.age = 10;
#pragma mark-----對自定義對象進行歸檔和反歸檔----
//第一步:創(chuàng)建一個NSMutableData 用于初始化歸檔工具
NSMutableData *data = [NSMutableData data];
//第二步:創(chuàng)建一個歸檔工具
NSKeyedArchiver *keyedArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//第三步:使用歸檔工具 對需要歸檔的對象進行歸檔
[keyedArchiver encodeObject:person forKey:@"person"];
NSLog(@"---%@",data);
//第四步:結束歸檔(只有在結束歸檔之后數(shù)據(jù)才能得到有效保存)
[keyedArchiver finishEncoding];
NSLog(@"---%@",data);
//寫入文件(確定具體的文件)
NSString *dataPath = [documentPathStr stringByAppendingPathComponent:@"data.plist"];
//寫入文件(寫入)
[data writeToFile:dataPath atomically:YES];
NSLog(@"%@",documentPathStr);
#pragma mark-----解檔---
//第一步:從本地獲取到的Data
NSData *newData = [NSData dataWithContentsOfFile:dataPath];
NSLog(@"newData = %@",newData);
//第二步:通過獲取到的data,創(chuàng)建解檔工具
NSKeyedUnarchiver *keyedUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:newData];
//第三步:創(chuàng)建一個person對象,接收解檔結果
Person *newPerson = [keyedUnarchiver decodeObjectForKey:@"person"];
//第四步:結束解檔
[keyedUnarchiver finishDecoding];
NSLog(@"person =====%@",newPerson.name);
//對Foundation框架中的對象進行歸檔和反歸檔
[self foundationClass]
//對自定義內(nèi)容進行歸檔和反歸檔
[self customContent]
}
#pragma mark ------- 對Foundation框架中的對象進行歸檔和反歸檔 --------
- (void)foundationClass{
//歸檔(以數(shù)組為例說明)
//1. 創(chuàng)建路徑
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
//2. 拼接一個文件
NSString *filePath = [document stringByAppendingString:@"/file.plist"];
//3. 創(chuàng)建歸檔對象
NSArray *array = @[@"C", @"OC", @"UI"];
//4. 歸檔
//archiveRootObject: 對基本數(shù)據(jù)類型數(shù)據(jù)進行歸檔的方法
BOOL result = [NSKeyedArchiver archiveRootObject:array toFile:filePath];
if (result) {
NSLog(@"array歸檔成功:%@", filePath);
}
//5. 不需要結束
//反歸檔
//1. 創(chuàng)建對象用來接收
NSArray *newArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"解檔結果 = %@", newArray);
}
#pragma mark ------- 對自定義內(nèi)容進行歸檔和反歸檔 --------
- (void)customContent{
//歸檔
//1. 獲取路徑
NSString *custonPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
custonPath = [custonPath stringByAppendingString:@"/customFile"];
//2. 使用Data對象存放歸檔數(shù)據(jù)
NSMutableData *data = [NSMutableData data];
//3. 創(chuàng)建歸檔對象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//4. 對自定義內(nèi)容進行歸檔
[archiver encodeObject:@"尼古拉斯.趙四" forKey:@"name"];
[archiver encodeObject:@"男" forKey:@"gender"];
[archiver encodeInt:22 forKey:@"age"];
[archiver encodeObject:@[@"AAAA", @"BBBB", @"CCCC"] forKey:@"array"];
//5. 完成歸檔
[archiver finishEncoding];
//6. 寫入本地
BOOL result = [data writeToFile:custonPath atomically:YES];
if (result) {
NSLog(@"寫入成功: %@", custonPath);
}
#warning <#message#>
//反歸檔:
//1. 讀取文件, 轉(zhuǎn)化成NSData對象類型
NSData *unarchiverData = [NSData dataWithContentsOfFile:custonPath];
//2. 創(chuàng)建反歸檔對象
NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:unarchiverData];
//3. 反歸檔(根據(jù)KEY值訪問)
NSString *str = [unArchiver decodeObjectForKey:@"name"];
NSLog(@"%@", str);
}
@end```