1 // 2 // ViewController.m 3 // IOS_0113_本地存儲 4 // 5 // Created by ma c on 16/1/13. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "BWLoginDataModel.h" 11 #import "BWSecondViewController.h" 12 13 @interface ViewController () 14 15 @end 16 17 @implementation ViewController 18 /* 19 數據本地化(數據持久化)- 本地存儲 20 1.NSUserDefault屬性列表存儲 - 輕量級數據,存儲類型有限 21 2.歸檔 - 大量數據,存儲類型能夠擴展到自定義對象 22 3.本地數據庫 23 24 無論是屬性列表仍是歸檔,都是將數據保存到後臺,前端任意一個界面都能訪問 25 */ 26 27 - (void)viewDidLoad { 28 [super viewDidLoad]; 29 /* 30 歸檔 - 本地化數據模型 31 <NSCoding> - 要對哪一個類的對象進行歸檔,就讓哪一個類實現這個協議 32 */ 33 self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; 34 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeView)]; 35 [self.view addGestureRecognizer:tap]; 36 37 BWLoginDataModel *loginModel = [[BWLoginDataModel alloc] init]; 38 loginModel.name = @"123456"; 39 loginModel.password = @"654321"; 40 41 //沙盒路徑 - 每個應用都有本身的沙盒,IOS機制 42 NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"account.data"]; 43 //NSLog(@"%@",filePath); 44 45 //將一個歸檔對象歸檔到目錄下 46 if ([NSKeyedArchiver archiveRootObject:loginModel toFile:filePath]) { 47 NSLog(@"歸檔成功"); 48 } 49 else 50 NSLog(@"歸檔失敗"); 51 52 } 53 54 - (void)changeView 55 { 56 BWSecondViewController *secondVC = [[BWSecondViewController alloc] init]; 57 58 [self presentViewController:secondVC animated:YES completion:nil]; 59 } 60 61 #pragma mark - 屬性列表基礎 62 - (void)useUserDefault 63 { 64 /* 65 //屬性列表NSUserDefault(全局惟一,不用建立) 66 67 某些數據當存儲本地以後,下次再次調用的時候,不用再次從新賦值,而是從本地直接獲取 68 若是沒有必要,別向屬性列表存儲大量數據 69 通常用來存儲簡單的全局都能使用到的數據 70 e.g. DeviceToken 設備令牌 71 SessionID 安全口令 72 ps:屬性列表的存儲和讀取速度都是及時的,而歸檔和數據庫都是有存儲延時的 73 pss:自動登陸 74 把用戶的帳號,密碼存儲到屬性列表中 75 76 */ 77 // NSString *str = @"bowen"; 78 // [[NSUserDefaults standardUserDefaults] setObject:str forKey:@"sister"]; 79 // NSString *newStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"sister"]; 80 // NSLog(@"%@",newStr); 81 // NSLog(@"%@",[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]); 82 83 //注意別刪除錯了 84 //[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"sister"]; 85 //NSLog(@"%@",newStr); 86 87 } 88 89 - (void)didReceiveMemoryWarning { 90 [super didReceiveMemoryWarning]; 91 // Dispose of any resources that can be recreated. 92 } 93 94 @end
1 // 2 // BWSecondViewController.m 3 // IOS_0113_本地存儲 4 // 5 // Created by ma c on 16/1/13. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import "BWSecondViewController.h" 10 #import "BWLoginDataModel.h" 11 12 @interface BWSecondViewController () 13 14 @end 15 16 @implementation BWSecondViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 self.view.backgroundColor = [UIColor cyanColor]; 21 22 //解檔 23 NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"account.data"]; 24 BWLoginDataModel *dataModel = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; 25 NSLog(@"%@",dataModel); 26 } 27 28 - (void)didReceiveMemoryWarning { 29 [super didReceiveMemoryWarning]; 30 // Dispose of any resources that can be recreated. 31 } 32 33 @end
1 // 2 // BWLoginDataModel.h 3 // IOS_0113_本地存儲 4 // 5 // Created by ma c on 16/1/13. 6 // Copyright (c) 2016年 博文科技. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface BWLoginDataModel : NSObject<NSCoding> 12 13 14 @property (nonatomic, copy) NSString *name; 15 @property (nonatomic, copy) NSString *password; 16 17 18 @end 19 20 21 // 22 // BWLoginDataModel.m 23 // IOS_0113_本地存儲 24 // 25 // Created by ma c on 16/1/13. 26 // Copyright (c) 2016年 博文科技. All rights reserved. 27 // 28 29 #import "BWLoginDataModel.h" 30 31 @implementation BWLoginDataModel 32 33 - (NSString *)description 34 { 35 return [NSString stringWithFormat:@"name:%@ password:%@", self.name,self.password]; 36 } 37 38 #pragma mark - 歸檔方法 39 - (void)encodeWithCoder:(NSCoder *)aCoder 40 { 41 //按照規定的Key對屬性進行編碼操做 42 [aCoder encodeObject:self.name forKey:@"name"]; 43 [aCoder encodeObject:self.password forKey:@"password"]; 44 45 } 46 #pragma mark - 解檔方法 47 - (id)initWithCoder:(NSCoder *)aDecoder 48 { 49 self = [super init]; 50 if (self) { 51 //歸檔和解檔的key能夠隨意寫,但他們屬性對應的key必須相同 52 _name = [aDecoder decodeObjectForKey:@"name"]; 53 _password = [aDecoder decodeObjectForKey:@"password"]; 54 } 55 return self; 56 } 57 58 @end