今天我們要搞一搞iOS開發(fā)當中的歸檔和反歸檔淆珊,在進入正題之前,我們需要了解一下歸檔和反歸檔是干什么用的奸汇。
所謂歸檔施符,最通用的定義就是存儲有組織的數(shù)據(jù)往声,目的是為了長時間存放有組織的數(shù)據(jù)集。實際上就是將對象寫入文件當中戳吝。下面我們學習第一種歸檔與反歸檔方法
一浩销、
(1)歸檔
NSArray *array = [NSArray arrayWithObjects:@1,@2,@3, nil];
NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"testFile.plist"];
BOOL success = [NSKeyedArchiver archiveRootObject:array toFile:filePath];
NSLog(@"%d",success);
NSLog(@"%@",filePath);
通過打印BOOL值來判斷是否歸檔成功,也可以打印文件路徑听哭,通過Finder查找到文件夾來查看歸檔結(jié)果慢洋。
(2)反歸檔
NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"testFile.plist"];
id array = [NSKeyedUnarchiver unarchiveObjectWithFile:filepath];
NSLog(@"%@",array);
第一種方法比較簡單,但是只能存儲單個對象陆盘。
第二種方法可以將多個對象歸檔成一個文件普筹。
二、
(1)歸檔
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"BigDragon",@"BigBiao",@"BigBaby", nil];
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//編碼
[archiver encodeObject:array forKey:@"array"];
[archiver encodeObject:@"Jason‘s friends" forKey:@"name"];
//編碼完成之后隘马,對象已經(jīng)存儲到data之中太防。
[archiver finishEncoding];
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"array.plist"];
BOOL success = [data writeToFile:filePath atomically:YES];
NSLog(@"%d",success);```
*(2)反歸檔*
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"array.plist"];
//讀取歸檔數(shù)據(jù)
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
//創(chuàng)建解歸檔對象,進行反歸檔
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
//反歸檔
NSArray *array = [unarchiver decodeObjectForKey:@"array"];
NSLog(@"%@",array);
NSString *name = [unarchiver decodeObjectForKey:@"name"];
NSLog(@"%@",name);```
前兩種方法我們歸檔的都是系統(tǒng)給定的類祟霍,那么如何歸檔我們自定義的類呢杏头?
(三)歸檔自定義對象
美好的一天從創(chuàng)建一個Person類開始。 ——洋哥
遵從洋哥的教誨沸呐,讓我們先創(chuàng)建一個Person類吧醇王。
自定義類的歸檔需要遵守NSCoding協(xié)議,廢話不多說崭添,還是看代碼吧寓娩。
Person.h
#import <Foundation/Foundation.h>
// 給自定義類歸檔,首先要遵守NSCoding協(xié)議呼渣。
@interface Person : NSObject<NSCoding>
@property (nonatomic,strong) NSString *name;
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,strong) NSString *gender;
- (NSString *)description;
@end```
*Person.m*
*.m文件中需要寫encodeWithCoder和initWithCoder兩個方法棘伴,分別在歸檔和反歸檔的時候調(diào)用。*
import "Person.h"
@implementation Person
// 歸檔方法
(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
[aCoder encodeObject:self.gender forKey:@"gender"];
}
// 反歸檔方法-
(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];if (self != nil) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
self.gender = [aDecoder decodeObjectForKey:@"gender"];
}
return self;
} (NSString *)description
{
NSString *string = [NSString stringWithFormat:@"%@,%ld,%@",self.name,self.age,self.gender];
return string;
}
@end```
main函數(shù)
寫完P(guān)erson的.h 和 .m 文件之后屁置,接下來的就是在main函數(shù)中的操作了焊夸,我這里用的是ViewController,效果是一樣的蓝角。
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person *person = [[Person alloc]init];
person.name = @"BigBaby";
person.age = 16;
person.gender = @"男";
// 歸檔阱穗,調(diào)用歸檔方法
NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"person.plist"];
BOOL success = [NSKeyedArchiver archiveRootObject:person toFile:filePath];
NSLog(@"%d",success);
// 反歸檔,調(diào)用反歸檔方法
Person *per = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",per);
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}```
*通過打印反歸檔解出來的BOOL值和對象per使鹅,可以驗證是否歸檔和反歸檔成功揪阶。*