初級數據持久化NSKeyedArchiver(二)

看代碼:atom

 1 #import "MainViewController.h"
 2 #import "Student.h"
 3 
 4 @interface MainViewController ()
 5 
 6 @end
 7 
 8 @implementation MainViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     // Do any additional setup after loading the view.
13     
14     
15     Student *student = [[Student alloc]initWithName:@"安妮" Sex:@"" HP:1800];
16     
17     
18     
19 
20     
21 #pragma mark --歸檔(序列化)
22     
23     //歸檔
24     //1.建立NSMUTableData
25     NSMutableData *data = [NSMutableData data];
26     
27     //2.建立一個歸檔對象
28     NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
29     
30     //3.用歸檔對象將自定義類的對象轉化成二進制數據流(NSData)
31     [archiver encodeObject:student forKey:@"student"];
32     
33     //4.歸檔完成
34     [archiver finishEncoding];
35     
36     //5.歸檔完成以後寫入本地
37     
38     NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
39     
40 //    //直接寫入路勁
41 //    [NSKeyedArchiver archiveRootObject:student toFile:[documentsPath stringByAppendingPathComponent:@"student.mp3"]];
42 
43     NSString *studentPath = [documentsPath stringByAppendingPathComponent:@"student.txt"];
44     
45     [data writeToFile:studentPath atomically:YES];
46     
47     NSLog(@"%@",studentPath);
48     
49 
50     
51     
52     
53     
54 #pragma mark --反歸檔(反序列化)
55     
56     //1.獲取存進去的二進制文件
57     NSMutableData *unData = [[NSMutableData alloc]initWithContentsOfFile:studentPath];
58     
59     //2.建立反歸檔對象
60     NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:unData];
61     
62     //3.將二進制文件轉化爲OC對象
63     
64     Student *stu = [unArchiver decodeObjectForKey:@"student"];
65     
66     NSLog(@"%@",stu.name);
67     
68     [unArchiver finishDecoding];
69     
70     
71 }

model類裏面怎麼寫了?繼續看下去spa

 1 #import <Foundation/Foundation.h>
 2 
 3 //必須簽定協議
 4 @interface Student : NSObject<NSCoding>
 5 
 6 @property (nonatomic,retain) NSString *name ;
 7 
 8 @property (nonatomic,retain) NSString *sex ;
 9 
10 @property (nonatomic,assign) int HP ;
11 
12 -(instancetype)initWithName:(NSString *)name Sex:(NSString *)sex HP:(int)HP ;
13 
14 
15 **********************************************
16 
17 #import "Student.h"
18 
19 @implementation Student
20 
21 -(void)dealloc{
22     [_name release];
23     [_sex release];
24     [super dealloc];
25 }
26 
27 
28 #pragma mark --解壓過程
29 -(id)initWithCoder:(NSCoder *)aDecoder{
30     self = [super init] ;
31     if (self) {
32         self.name = [aDecoder decodeObjectForKey:@"name"];
33         self.sex = [aDecoder decodeObjectForKey:@"sex"] ;
34         self.HP = [aDecoder decodeIntForKey:@"HP"] ;
35     }
36     
37     
38     return self ;
39 }
40 
41 #pragma  mark --壓縮過程
42 -(void)encodeWithCoder:(NSCoder *)aCoder{
43     
44     //轉化的是自身的屬性,壓縮過程
45     [aCoder encodeObject:self.name forKey:@"name"] ;
46     [aCoder encodeObject:self.sex forKey:@"sex"] ;
47     [aCoder encodeInt:self.HP forKey:@"HP"] ;
48     
49     
50 }
51 
52 
53 
54 
55 -(instancetype)initWithName:(NSString *)name Sex:(NSString *)sex HP:(int)HP{
56     
57     if (self = [super init]) {
58         self.name = name ;
59         self.sex = sex ;
60         self.HP = HP ;
61     }
62     
63     return self ;
64     
65     
66 }
相關文章
相關標籤/搜索