04-アーカイブのアーカイブ解除
3725 ワード
アーカイブ解除手順:
1、プロトコルに従う:2、符号化復号を実現する2つの方法//符号化(オブジェクト->バイナリ):システムに、アーカイブ時に保存する属性-(void)encodeWithCoder:(NSCoder*)aCoder{}//復号(バイナリ->オブジェクト):システムに、アーカイブ時にそれらの属性-(nullable instancetype)initWithCoder:(NSCoder*)aDecoder{
3、アーカイブのアーカイブ解除方法//2を呼び出す.プロトコルを実装するencodeWithCoder:メソッドは、どのプロパティを保存するかを教えます[NSKeydArchiver archiveRootObject:person toFile:filePath];//2.アーカイブGEPerson*person=[NSKeydUnarchiver unarchiveObjectWithFile:filePath];
/*********** *********** GEPerson *********** ***********/
// GEPerson.h
//
// 1.
// 2.
#import
#import "GEComputer.h"
// 1.
@interface GEPerson : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) NSInteger age;
@property (nonatomic,strong) GEComputer *computer;
@end
// GEPerson.m
//
#import "GEPerson.h"
@implementation GEPerson
// 2.
// ( -> ): , ,
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
[aCoder encodeObject:_computer forKey:@"computer"];
}
// ( -> ): , ,
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
_name = [aDecoder decodeObjectForKey:@"name"];
_age = [aDecoder decodeIntegerForKey:@"age"];
_computer = [aDecoder decodeObjectForKey:@"computer"];
}
return self;
}
@end
/*********** *********** GEComputer *********** ***********/
// GEComputer.h
//
#import
@interface GEComputer : NSObject
@property (nonatomic,copy) NSString *systemName;
@end
// GEComputer.m
//
#import "GEComputer.h"
@implementation GEComputer
-(void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:_systemName forKey:@"systemName"];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
_systemName = [aDecoder decodeObjectForKey:@"systemName"];
}
return self;
}
@end
/*********** *********** ViewController *********** ***********/
// ViewController.m
//
#import "ViewController.h"
#import "GEPerson.h"
#import "GEComputer.h"
@interface ViewController ()
@end
@implementation ViewController
/**
:
*/
- (void)viewDidLoad {
[super viewDidLoad];
//
NSLog(@" :%@",NSHomeDirectory());
//
[self saveData];
//
[self readData];
}
//
-(void)saveData {
// 1.
GEComputer *computer = [GEComputer new];
computer.systemName = @"Mac OS";
GEPerson *person = [GEPerson new];
person.name = @"Jack";
person.age = 18;
person.computer = computer;
// 2.
// 2.1
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documents stringByAppendingPathComponent:@"person.data"];
// 2.2 encodeWithCoder: ,
[NSKeyedArchiver archiveRootObject:person toFile:filePath];
}
//
-(void)readData {
// 1.
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documents stringByAppendingPathComponent:@"person.data"];
// 2.
GEPerson *person = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@" : = %@, = %ld, = %@",person.name,person.age,person.computer.systemName);
}
@end