一.封裝 1.封裝的概念: 封裝,顧名思義,就是把什麼封起來,裝起來.在程式開發中,封裝是優化代碼,提高代碼安全性的一種做法,即是將不必要暴露在外界的代碼,封裝在一個類中,在外界只調用他.如果看到這裡還沒有明白的話,那麼請往下看: 比如:你買了一個手機,你可以用手機打電話,發信息,上網等等各種操作, ...
一.封裝
1.封裝的概念:
封裝,顧名思義,就是把什麼封起來,裝起來.在程式開發中,封裝是優化代碼,提高代碼安全性的一種做法,即是將不必要暴露在外界的代碼,封裝在一個類中,在外界只調用他.如果看到這裡還沒有明白的話,那麼請往下看:
比如:你買了一個手機,你可以用手機打電話,發信息,上網等等各種操作,但是你需要知道手機怎麼來實現這個功能的嗎?當然不用,你只需要知道怎麼使用它.再例如在開發中可能會用到第三方庫,比如你去Github上下了一個MJRefresh的庫,你需要知道這個庫是怎麼實現的麽?不用,你只需要知道你該怎麼使用它,這就是封裝.
2.代碼:
這裡舉例用的是字典轉模型數據
2.1 KXZCar.h
1 // 2 // KXZCar.h 3 // 封裝 4 // 5 // Created by admin on 16/4/28. 6 // Copyright © 2016年 admin. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface KXZCar : NSObject 12 /** 13 * 汽車名稱 14 */ 15 @property (nonatomic,copy) NSString *name; 16 17 /** 18 * 汽車的圖標 19 */ 20 @property (nonatomic,copy) NSString *icon; 21 /** 22 * 汽車信息 23 */ 24 @property (nonatomic, copy) NSString *info; 25 /** 26 * 產地 27 */ 28 @property (nonatomic, copy) NSString *country; 29 //實現字典轉模型的方法 30 // 對象方法 31 - (instancetype) initWithDict:(NSDictionary *) dict; 32 33 //類方法 34 + (instancetype) carWithDict:(NSDictionary *) dict; 35 @end
2.2 KXZCar.m
1 // 2 // KXZCar.m 3 // 封裝 4 // 5 // Created by admin on 16/4/28. 6 // Copyright © 2016年 admin. All rights reserved. 7 // 8 9 #import "KXZCar.h" 10 11 @implementation KXZCar 12 - (instancetype)initWithDict:(NSDictionary *)dict 13 { 14 if (self = [super init]) { 15 16 self.icon = dict[@"icon"]; 17 self.name = dict[@"name"]; 18 self.country = dict[@"country"]; 19 self.info = dict[@"info"]; 20 // KVC賦值 21 // [self setValuesForKeysWithDictionary:dict]; 22 23 } 24 return self; 25 } 26 27 + (instancetype)carWithDict:(NSDictionary *)dict 28 { 29 return [[self alloc] initWithDict:dict]; 30 } 31 @end
2.3 KXZCarGroup.h
1 // 2 // KXZCarGroup.h 3 // 封裝 4 // 5 // Created by admin on 16/4/28. 6 // Copyright © 2016年 admin. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "KXZCar.h" 11 12 @interface KXZCarGroup : NSObject 13 //標題 14 @property (nonatomic,copy) NSString *title; 15 //汽車數組 16 @property (nonatomic,strong) NSArray *cars; 17 18 //提供兩個字典轉模型的方法 19 //對象方法 20 - (instancetype) initWithDict:(NSDictionary *) dict; 21 //類方法 22 + (instancetype) carGroupWithDict:(NSDictionary *) dict; 23 24 //載入plist文件,把字典數組轉換為模型數組 25 + (NSArray *) carGroups; 26 @end
2.4 KXZCarGroup.m
1 // 2 // KXZCarGroup.m 3 // 封裝 4 // 5 // Created by admin on 16/4/28. 6 // Copyright © 2016年 admin. All rights reserved. 7 // 8 9 #import "KXZCarGroup.h" 10 11 @implementation KXZCarGroup 12 - (instancetype)initWithDict:(NSDictionary *)dict 13 { 14 if (self = [super init]) { 15 self.title = dict[@"title"]; 16 self.cars = dict[@"cars"]; 17 } 18 return self; 19 } 20 21 22 + (instancetype)carGroupWithDict:(NSDictionary *)dict 23 { 24 return [[self alloc] initWithDict:dict]; 25 } 26 27 28 + (NSArray *)carGroups 29 { 30 // 1.載入plist 31 // 獲取絕對路徑 32 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"]; 33 // 讀取數組(分組的字典) 34 NSArray *array = [NSArray arrayWithContentsOfFile:filePath]; 35 36 // 2.把array中分組的字典轉為模型 37 // 2.1 定義一個可變數組 38 NSMutableArray *arrayM = [NSMutableArray array]; 39 // 遍歷array,把它裡面存放的組字典轉換為組模型,放到arrayM中 40 for (NSDictionary *dict in array) { 41 KXZCarGroup *carGroup = [self carGroupWithDict:dict]; 42 // 這個字典數組 43 NSArray *dictArray = carGroup.cars; 44 // 把dictArray轉換為一個CZCar的模型數組 45 // 定義一個可變數組用來存放轉換後的CZCar模型 46 NSMutableArray *carsArray = [NSMutableArray array]; 47 // 遍歷字典數組dictArray,把字典轉換為CZCar的模型 48 for (NSDictionary *dict in dictArray) { 49 // 把字典轉換為CZCar的模型 50 KXZCar *car = [KXZCar carWithDict:dict]; 51 // 把轉換後的car添加到carsArray數組中 52 [carsArray addObject:car]; 53 } 54 // 把轉換後的CZCar的模型數組賦值給carGroup的cars屬性 55 carGroup.cars = carsArray; 56 57 [arrayM addObject:carGroup]; 58 } 59 // 3.返回組模型數組 60 return arrayM; 61 } 62 @end
3.plist文件如下:
4.BCViewController(就是一個普通的ViewController)
4.1代碼如下:
1 // 2 // BCViewController.m 3 // 封裝 4 // 5 // Created by admin on 16/4/28. 6 // Copyright © 2016年 admin. All rights reserved. 7 // 8 9 #import "BCViewController.h" 10 #import "KXZTableViewCell.h" 11 #import "KXZCar.h" 12 #import "KXZCarGroup.h" 13 14 @interface BCViewController ()<UITableViewDelegate,UITableViewDataSource> 15 @property (nonatomic, strong) UITableView *tableView; 16 @property (nonatomic, strong) UILabel *headLabel; 17 @property (nonatomic, strong) UIButton *clickButton; 18 @property (nonatomic, strong) NSArray *carGroups; 19 @end 20 21 @implementation BCViewController 22 23 -(NSArray *)carGroups{ 24 if (_carGroups == nil) { 25 _carGroups = [KXZCarGroup carGroups]; 26 } 27 return _carGroups; 28 } 29 @end
註:以為只介紹封裝,故不發tableView的實現了.
4.2說明:
在以上的代碼2.4中的38行開始,我定義了一個類方法,在類方法中實現了字典轉模型,然後在代碼4.1中的23行到28行的懶載入(延時載入)中,判斷數組是否為空,如果為空,就調用KXZCarGroup的類方法.在類方法中字典轉模型,外界(BCViewController中)我只需要調用一下這個類的方法去達到給數組carGroups賦值的目的,而不用知道裡面是怎麼去把字典轉成模型數據.
5.總結
封裝就是對一些不想要外界訪問到的成員變數,屬性和方法進行保護,不讓外界知道你內部是怎麼實現的.一般在封裝的過程中,還可以使用幾個關鍵字:protected(受保護的)-private(私有的)-public(公共的),如有不足,請兄弟們補充.