iOS MJExtension架構之字典數組轉模型數組

來源:互聯網
上載者:User

標籤:

1.下面這樣的數組,怎麼轉成模型

   

 

2.分析

3.新增模型:城市分組,城市,區,每個模型都有name,抽一個父類

4.每個模型分別具備的屬性

BaseModel : NSObject    屬性 NSString *name

CitySection : BaseModel   屬性 NSArray *cities

City : BaseModel      屬性 NSArray *districts; 屬性 BOOL hot;

District : BaseModel     屬性  NSArray *neighborhoods

5.控制器

5.1 匯入架構 #import "MJExtension.h"

5.2 控制器的成員屬性 : @property (nonatomic,strong) NSArray *citySections;

5.3 消極式載入

 1 - (NSArray *)citySections 2 { 3     if (_citySections == nil) { 4         NSString *path = [[NSBundle mainBundle] pathForResource:@"Cities.plist" ofType:nil]; 5         NSArray *dataArray = [NSArray arrayWithContentsOfFile:path]; 6         NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dataArray.count]; 7          8         for (NSDictionary *dict in dataArray) { 9         // MJExtension架構裡,字典轉模型的方法10            CitySection *section = [CitySection objectWithKeyValues:dict];11             [arrayM addObject:section];12         }     13         _citySections = arrayM;14     }15 16     return  _citySections;17 }

6 在模型類的.m檔案中

6.1匯入架構  #import "MJExtension.h"

6.2 實現方法

目的是告訴控制器的  objectWithKeyValues: 方法 , cities數組裡面裝的時city模型

// 在CitySection.m中- (NSDictionary *)objectClassInArray{    return @{ @"cities" : [City class] };}
// 在 City.m中- (NSDictionary *)objectClassInArray{    return @{ @"districts" : [District class]};}

注:District.m中,就不用這麼幹了,因為 District模型的屬性 NSArray *neighborhoods 裡裝的是NSString 

7 看看轉換的結果

 

iOS MJExtension架構之字典數組轉模型數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.