一、歸檔的基本概念
之前將數(shù)據(jù)保存本地,只能是字符串、數(shù)組、字典、NSNuber、BOOL等容器類對象對象,不能將所有對象都給保存,而采用歸檔能將所有的對象轉化為二進制數(shù)據(jù)保存在文件中,并通過解歸檔讓將文件里面保存的數(shù)據(jù)讀取出來
二、使用環(huán)境
之前我們給通訊錄添加一個聯(lián)系人只能是將添加的人放到一個字典中,然后將這個字典放到數(shù)組中,最終將數(shù)組寫入文件中
當我們需要顯示這些聯(lián)系人時,要從文件中將這個數(shù)組讀取出來,還要將數(shù)據(jù)里面的一個個字典轉化成model,放到一個新數(shù)組里
而現(xiàn)在我們可以使用歸檔在添加的時候就將這一個個聯(lián)系人的信息轉化成model,將這些model直接放到一個數(shù)組里,需要展示的時候,在從文件中讀取出來數(shù)據(jù),此時這個數(shù)組里面存放直接就是一個個model
有些應用支持一個離線緩存,也就是說當手機沒聯(lián)網(wǎng)時,可以將手機有網(wǎng)時的數(shù)據(jù)存放在本地,當手機沒網(wǎng)時,從本地中取出來這些數(shù)據(jù)展示
三、某個對象支持歸檔解歸檔需要滿足三個條件
1、所屬的類遵守NSCoding協(xié)議
2、實現(xiàn)協(xié)議里面的歸檔方法
- (void)encodeWithCoder:(NSCoder *)aCoder
3、實現(xiàn)協(xié)議里面的解歸檔方法
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
四、對系統(tǒng)的類進行歸檔解歸檔
1、指定將對象放在哪個文件中,歸檔后的文件,后綴要求是archiver
[NSHomeDirectory() stringByAppendingPathComponent:@"data.archiver"];
2、將對象歸檔到指定的路徑中
[NSKeyedArchiver archiveRootObject:name toFile:path];
3、將歸檔后的數(shù)據(jù)提取出來
[NSKeyedUnarchiver unarchiveObjectWithFile:path];
五、對自定義的類進行歸檔與解歸檔? ??
1、讓這個類遵循
2、實現(xiàn)歸檔方法,aCoder就是歸檔時傳過來的歸檔對象,對象被歸檔時會調用這個方法
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeObject:self.sex forKey:@"sex"];
}
3、實現(xiàn)解歸檔方法,對象解歸檔是會調用這個方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
//解歸檔時會產生一個Person對象,這里是給這個Person對象賦值
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
self.sex = [aDecoder decodeObjectForKey:@"sex"];
}
return self;
}
六、同時將多個對象歸檔與解歸檔
1、歸檔
1)準備一個可變的data對象,通過歸檔對象將多個數(shù)據(jù)存在一個data對象里,最終將這個data寫入文件
NSMutableData *data = [NSMutableData data];
2)archiver初始化的時候包裝一個可變的data對象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
3)通過歸檔對象將這些數(shù)據(jù)轉化成二進制,并保存在一個data對象里
[archiver encodeObject:name forKey:@"name"];
[archiver encodeInteger:age forKey:@"age"];
[archiver encodeObject:sex forKey:@"sex"];
4)轉化完畢,意思是結束使用歸檔對象將上面的數(shù)據(jù)保存在了data里面
[archiver finishEncoding];
5)將轉化好的data寫入文件
[data writeToFile:path atomically:YES];
2、解歸檔
1)將路徑里的二進制數(shù)據(jù)給取出來
NSMutableData *data = [NSMutableData dataWithContentsOfFile:path];
2)將二進制數(shù)據(jù)包裝在一個解歸檔對象中
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
3)通過解歸檔對象將二進制數(shù)據(jù)分別給反序列化
NSString *name = [unarchiver decodeObjectForKey:@"name"];
NSInteger age = [unarchiver decodeIntegerForKey:@"age"];
NSString *sex = [unarchiver decodeObjectForKey:@"sex"];
七、練習??
1、模擬網(wǎng)絡數(shù)據(jù)進行本地緩存? ?
?1)修改新工程自帶的ViewController.h 如下? ? ?
?#import<UIKit/UIKit.h>
@interface ViewController : UITableViewController
@end
2)在AppDelegate.m里面自定義window,
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
//報錯后,記得導入ViewController? #import "ViewController.h"
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
3)新建一個InfoModel類,? ? ??
InfoModel.h? ? ??
? #import<Foundation/Foundation.h>
//對象要歸檔必須要遵守NSCoding協(xié)議? ? ? ?
?@interface InfoModel : NSObject<NSCoding>
@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *phone;
@end
InfoModel.m
#import "InfoModel.h"
@implementation InfoModel
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.phone forKey:@"phone"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.phone = [aDecoder decodeObjectForKey:@"phone"];
}
return self;
}
@end
4) ViewController.m
#import "ViewController.h"
#import "InfoModel.h"
//報錯,將課件中的MJExtension文件夾拖到工程中
#import "MJExtension.h"
//模擬服務器路徑
#define kLocalPath [NSHomeDirectory() stringByAppendingPathComponent:@"data.archiver"]
//模擬本地緩存路徑
#define kServerPath [[NSBundle mainBundle] pathForResource:@"Connect" ofType:@"plist"]
@interface ViewController ()
{
NSMutableArray *dataArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadData];
}
- (void)loadData{
//準備數(shù)據(jù)
dataArray = [self fetchData];
if(dataArray == nil){
NSLog(@"請檢查網(wǎng)絡設置");
return;
}
[self.tableView reloadData];
}
- (NSMutableArray *)fetchData{
//1、先從服務器獲取數(shù)據(jù)
NSMutableArray *tempArray = [NSMutableArray arrayWithContentsOfFile:kServerPath];
if (tempArray == nil) {
//2、如果從服務器獲取數(shù)據(jù)失敗,則從本地緩存中讀取數(shù)據(jù)
tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:kLocalPath];
}else{
//3、如果從服務器獲取數(shù)據(jù)成功,則將數(shù)據(jù)通過MJExtension框架,轉化為model
tempArray = [InfoModel mj_objectArrayWithKeyValuesArray:tempArray];
//4、將最新從服務器獲取到數(shù)據(jù)保存到本地
[NSKeyedArchiver archiveRootObject:tempArray toFile:kLocalPath];
}
return tempArray;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
}
InfoModel *model = dataArray[indexPath.row];
cell.textLabel.text = model.name;
cell.detailTextLabel.text = model.phone;
return cell;
}
@end