iOS開發UI篇—ios應用資料存儲方式(歸檔)
一、簡單說明
在使用plist進行資料存儲和讀取,隻适用于系統自帶的一些常用類型才能用,且必須先擷取路徑相對麻煩;
偏好設定(将所有的東西都儲存在同一個檔案夾下面,且主要用于存儲應用的設定資訊)
歸檔:因為前兩者都有一個緻命的缺陷,隻能存儲常用的類型。歸檔可以實作把自定義的對象存放在檔案中。
二、代碼示例
1.檔案結構
2.代碼示例
YYViewController.m檔案
1 //
2 // YYViewController.m
3 // 02-歸檔
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10 #import "YYPerson.h"
11
12 @interface YYViewController ()
13 - (IBAction)saveBtnOnclick:(id)sender;
14 - (IBAction)readBtnOnclick:(id)sender;
15
16 @end
17
18 @implementation YYViewController
19
20 - (void)viewDidLoad
21 {
22 [super viewDidLoad];
23 }
24
25
26 - (IBAction)saveBtnOnclick:(id)sender {
27 //1.建立對象
28 YYPerson *p=[[YYPerson alloc]init];
29 p.name=@"文頂頂";
30 p.age=23;
31 p.height=1.7;
32
33 //2.擷取檔案路徑
34 NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
35 NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
36 NSLog(@"path=%@",path);
37
38 //3.将自定義的對象儲存到檔案中
39 [NSKeyedArchiver archiveRootObject:p toFile:path];
40
41 }
42
43 - (IBAction)readBtnOnclick:(id)sender {
44 //1.擷取檔案路徑
45 NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
46 NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
47 NSLog(@"path=%@",path);
48
49 //2.從檔案中讀取對象
50 YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
51 NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
52 }
53 @end
建立一個person類
YYPerson.h檔案
1 //
2 // YYPerson.h
3 // 02-歸檔
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 // 如果想将一個自定義對象儲存到檔案中必須實作NSCoding協定
12 @interface YYPerson : NSObject<NSCoding>
13
14 //姓名
15 @property(nonatomic,copy)NSString *name;
16 //年齡
17 @property(nonatomic,assign)int age;
18 //身高
19 @property(nonatomic,assign)double height;
20 @end
YYPerson.m檔案
1 //
2 // YYPerson.m
3 // 02-歸檔
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYPerson.h"
10
11 @implementation YYPerson
12
13 // 當将一個自定義對象儲存到檔案的時候就會調用該方法
14 // 在該方法中說明如何存儲自定義對象的屬性
15 // 也就說在該方法中說清楚存儲自定義對象的哪些屬性
16 -(void)encodeWithCoder:(NSCoder *)aCoder
17 {
18 NSLog(@"調用了encodeWithCoder:方法");
19 [aCoder encodeObject:self.name forKey:@"name"];
20 [aCoder encodeInteger:self.age forKey:@"age"];
21 [aCoder encodeDouble:self.height forKey:@"height"];
22 }
23
24 // 當從檔案中讀取一個對象的時候就會調用該方法
25 // 在該方法中說明如何讀取儲存在檔案中的對象
26 // 也就是說在該方法中說清楚怎麼讀取檔案中的對象
27 -(id)initWithCoder:(NSCoder *)aDecoder
28 {
29 NSLog(@"調用了initWithCoder:方法");
30 //注意:在構造方法中需要先初始化父類的方法
31 if (self=[super init]) {
32 self.name=[aDecoder decodeObjectForKey:@"name"];
33 self.age=[aDecoder decodeIntegerForKey:@"age"];
34 self.height=[aDecoder decodeDoubleForKey:@"height"];
35 }
36 return self;
37 }
38 @end
3.列印效果和兩個重要的錯誤提示
點選儲存按鈕和讀取按鈕,成功列印結果如下:
關于不實作兩個協定方法的錯誤提示:
-(void)encodeWithCoder:(NSCoder *)aCoder方法:
-(id)initWithCoder:(NSCoder *)aDecoder方法:
三、繼承類中的使用
建立一個學生類,讓這個類繼承自Preson這個類,增加一個體重的屬性。
YYstudent.h檔案
1 //
2 // YYstudent.h
3 // 02-歸檔
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYPerson.h"
10
11 @interface YYstudent : YYPerson
12 //增加一個體重屬性
13 @property(nonatomic,assign) double weight;
14 @end
YYstudent.m檔案
1 //
2 // YYstudent.m
3 // 02-歸檔
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYstudent.h"
10
11 @implementation YYstudent
12
13 //在子類中重寫這兩個方法
14 - (void)encodeWithCoder:(NSCoder *)aCoder
15 {
16 [super encodeWithCoder:aCoder];
17 NSLog(@"調用了YYStudent encodeWithCoder");
18 [aCoder encodeFloat:self.weight forKey:@"weight"];
19 }
20
21 - (id)initWithCoder:(NSCoder *)aDecoder
22 {
23 if (self = [super initWithCoder:aDecoder]) {
24 NSLog(@"調用了YYstudent initWithCoder");
25 self.weight = [aDecoder decodeFloatForKey:@"weight"];
26 }
27 return self;
28 }
29 @end
YYViewController.m檔案
1 //
2 // YYViewController.m
3 // 02-歸檔
4 //
5 // Created by apple on 14-6-7.
6 // Copyright (c) 2014年 itcase. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10 #import "YYPerson.h"
11 #import "YYstudent.h"
12
13 @interface YYViewController ()
14 - (IBAction)saveBtnOnclick:(id)sender;
15 - (IBAction)readBtnOnclick:(id)sender;
16
17 @end
18
19 @implementation YYViewController
20
21 - (void)viewDidLoad
22 {
23 [super viewDidLoad];
24 }
25
26
27 - (IBAction)saveBtnOnclick:(id)sender {
28 //1.建立對象
29 // YYPerson *p=[[YYPerson alloc]init];
30 // p.name=@"文頂頂";
31 // p.age=23;
32 // p.height=1.7;
33
34 YYstudent *s=[[YYstudent alloc]init];
35 s.name=@"wendingding";
36 s.age=23;
37 s.height=1.7;
38 s.weight=62;
39 //2.擷取檔案路徑
40 NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
41 NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
42 NSLog(@"path=%@",path);
43
44 //3.将自定義的對象儲存到檔案中
45 // [NSKeyedArchiver archiveRootObject:p toFile:path];
46 [NSKeyedArchiver archiveRootObject:s toFile:path];
47
48 }
49
50 - (IBAction)readBtnOnclick:(id)sender {
51 //1.擷取檔案路徑
52 NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
53 NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
54 NSLog(@"path=%@",path);
55
56 //2.從檔案中讀取對象
57 // YYPerson *p=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
58 // NSLog(@"%@,%d,%.1f",p.name,p.age,p.height);
59 YYstudent *s=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
60 NSLog(@"%@,%d,%.1f,%f",s.name,s.age,s.height,s.weight);
61 }
62 @end
點選儲存按鈕和讀取按鈕後的列印輸出:
四、重要說明
1.儲存資料過程:
//1.建立對象
YYstudent *s=[[YYstudent alloc]init];
s.name=@"wendingding";
s.age=23;
s.height=1.7;
s.weight=62;
//2.擷取檔案路徑
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
NSLog(@"path=%@",path);
//3.将自定義的對象儲存到檔案中
[NSKeyedArchiver archiveRootObject:s toFile:path];
2.讀取資料過程:
//1.擷取檔案路徑
NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];
//2.從檔案中讀取對象
YYstudent *s=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
3.遵守NSCoding協定,并實作該協定中的兩個方法。
4.如果是繼承,則子類一定要重寫那兩個方法。因為person的子類在存取的時候,會去子類中去找調用的方法,沒找到那麼它就去父類中找,是以最後儲存和讀取的時候新增加的屬性會被忽略。需要先調用父類的方法,先初始化父類的,再初始化子類的。
5.儲存資料的檔案的字尾名可以随意命名。
6.通過plist儲存的資料是直接顯示的,不安全。通過歸檔方法儲存的資料在檔案中打開是亂碼的,更安全。