iOS開發--BaseModel,ios--basemodel

來源:互聯網
上載者:User

iOS開發--BaseModel,ios--basemodel

  軟體開發過程中,需要解析各種各樣的資料.如最基礎的plist檔案,從網路下載擷取的json資料,以及XML網頁資料,還有比較重要的Core Data資料.

  下面我來分享一種快速將json檔案中的字典轉為模型的方法.雖然很多人在用第三方類庫解析json資料,不過將json檔案中的字典轉為模型內部的實現原理還是需要瞭解一下的.

  下面是BaseModel.h聲明檔案,向外界開方了兩個方法:

  第7行的方法是實現json檔案中的字典轉模型;

  第10行方法是防止出現特殊類型的資料無法轉為模型而導致程式異常.

 1 BaseModel.h 2  3 #import <Foundation/Foundation.h> 4  5 @interface BaseModel : NSObject 6  7 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary; 8  9 // 將json中的value值交給model的屬性(覆寫該方法可以將json檔案中特殊的資料加到model中)特殊類型如:NSNull類型資料10 - (void)setAttributesWithDictionary:(NSDictionary *)jsonDict;11 12 @end

  

  下面是BaseModel.m實現檔案

 

 1 BaseModel.m 2  3 #import "BaseModel.h" 4  5 @implementation BaseModel 6  7 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary 8 { 9     self = [super init];10     11     if (self) {12         // 將jsonDictionary字典的value值交給model的屬性值13         [self setAttributesWithDictionary:jsonDictionary];14     }15     return self;16 }17 18 // 將jsonDictionary字典的value值交給model的屬性值19 - (void)setAttributesWithDictionary:(NSDictionary *)jsonDictionary20 {21     // 擷取映射關係22     NSDictionary *modelDictionary = [self attributesModel:jsonDictionary];23     24     for (NSString *jsonKey in modelDictionary) {25         // 取得屬性名稱26         NSString *modelAttributesNmae = [modelDictionary objectForKey:jsonKey];27         28         // 取得Value值29         id jsonValue = [jsonDictionary objectForKey:jsonKey];30         31         // 擷取屬性的setter方法32         SEL setterSEL = [self stringToSEL:modelAttributesNmae];33         34         // 如果Value值為NULL,則賦值為""(什麼也沒有)35         if ([jsonValue isKindOfClass:[NSNull class]]) {36             jsonValue = @"";37         }38         39         // Warning:PerformSelector may cause a leak because its selector is unknown(PerformSelector可能導致記憶體流失,因為它選取器是未知的)40         if ([self respondsToSelector:setterSEL]) {41             [self performSelector:setterSEL withObject:jsonValue];42         }43         44     }45 }46 47 // 將字典中的key和model的屬性名稱映射48 - (NSDictionary *)attributesModel:(NSDictionary *)jsonDictionary49 {50     NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];51     52     // 屬性名稱和jsonDictionary的key一樣53     for (NSString *jsonKey in jsonDictionary) {54         [dict setValue:jsonKey forKey:jsonKey];55     }56     return dict;57 }58 59 // 擷取屬性的setter方法60 - (SEL)stringToSEL:(NSString *)modelAttributes61 {62     // 截取屬性名稱的首字母63     NSString *firstString = [modelAttributes substringToIndex:1];64     65     // 首字母大寫66     firstString = [firstString uppercaseString];67     68     // 截取除首字母以外的其它屬性名稱內容69     NSString *endString = [modelAttributes substringFromIndex:1];70     71     // 拼接setter方法名72     NSString *selString = [NSString stringWithFormat:@"set%@%@:",firstString, endString];73     74     // 將字串轉化為方法75     SEL selector = NSSelectorFromString(selString);76     77     return selector;78 }79 80 @end

 

  其中BaseModel.m實現檔案的第41行會報一個警告,警告的內容上面已經給出了.

  到此為止,一個將json檔案中的字典轉為模型的類已經實現了.

  BaseModel的使用大家可以自己測試一下,在這裡就不在示範了.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.