标签:
模型
1 // 2 // YSFoodTypeModule.h 3 // YSUiSplitViewController 4 // 5 // Created by ys on 15/12/12. 6 // Copyright (c) 2015年 ys. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface YSFoodTypeModule : NSObject 12 13 @property (nonatomic,copy)NSString *idstr; 14 15 @property (nonatomic,copy)NSString *name; 16 //若使用MJExtension则不用写以下两个方法 17 -(instancetype)initWithDict:(NSDictionary *)dict; 18 19 +(instancetype)foodTypeWithDict:(NSDictionary *)dict; 20 21 @end
模型对应的.m文件
1 // 2 // YSFoodTypeModule.m 3 // YSUiSplitViewController 4 // 5 // Created by ys on 15/12/12. 6 // Copyright (c) 2015年 ys. All rights reserved. 7 // 8 9 #import "YSFoodTypeModule.h" 10 11 @implementation YSFoodTypeModule 12 13 +(instancetype)foodTypeWithDict:(NSDictionary *)dict 14 { 15 return [[self alloc]initWithDict:dict]; 16 } 17 18 -(instancetype)initWithDict:(NSDictionary *)dict 19 { 20 if (self = [super init]) { 21 22 self.name = dict[@"name"]; 23 self.idstr = dict[@"idstr"]; 24 // //or KVC,其缺点是字典键在模型中必须有对应属性,否则崩溃,而MJExtension则可以解决这个问题 25 // [self setValuesForKeysWithDictionary:dict]; 26 } 27 return self; 28 } 29 30 @end
控制器代码
1 // 2 // YSFoodTypesTableViewController.m 3 // YSUiSplitViewController 4 // 5 // Created by ys on 15/12/12. 6 // Copyright (c) 2015年 ys. All rights reserved. 7 // 8 9 #import "YSFoodTypesTableViewController.h" 10 #import "YSFoodTypeModule.h" 11 #import "MJExtension.h" 12 13 @interface YSFoodTypesTableViewController () 14 15 @property (nonatomic,strong)NSArray *foodTypes; 16 17 @end 18 19 20 @implementation YSFoodTypesTableViewController 21 22 -(NSArray *)foodTypes 23 { 24 if (_foodTypes == nil) { 25 //这里是字典转模型的几个方法 26 27 //----0,调用模型内部里的字典转模型方法 28 // NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"food_types.plist" ofType:nil]]; 29 // NSMutableArray *arrayM = [NSMutableArray array]; 30 // for (NSDictionary *dict in dictArray) { 31 // YSFoodTypeModule *foodT = [YSFoodTypeModule foodTypeWithDict:dict]; 32 // [arrayM addObject:foodT]; 33 // } 34 // _foodTypes = arrayM; 35 36 //----1,用MJExtension里的字典转模型方法,不用自己再写字典转模型方法.mj_objectWithKeyValues:dict 37 // NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"food_types.plist" ofType:nil]]; 38 // NSMutableArray *arrayM = [NSMutableArray array]; 39 // for (NSDictionary *dict in dictArray) { 40 // YSFoodTypeModule *foodT = [YSFoodTypeModule mj_objectWithKeyValues:dict]; 41 // [arrayM addObject:foodT]; 42 // } 43 // _foodTypes = arrayM; 44 45 //----2,用MJExtension里的字典数组转模型数组方法,不用自己再写字典转模型方法.mj_objectArrayWithKeyValuesArray:dictArray 46 // NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"food_types.plist" ofType:nil]]; 47 // _foodTypes = [YSFoodTypeModule mj_objectArrayWithKeyValuesArray:dictArray]; 48 49 //----3,用MJExtension里的字典数组转模型数组方法,不用自己再写字典转模型方法.mj_objectArrayWithFile:filePath 50 // NSString *filePath = [[NSBundle mainBundle] pathForResource:@"food_types.plist" ofType:nil]; 51 // _foodTypes = [YSFoodTypeModule mj_objectArrayWithFile:filePath]; 52 53 //----4,用MJExtension里的字典数组转模型数组方法,不用自己再写字典转模型方法.mj_objectArrayWithFilename 54 _foodTypes = [YSFoodTypeModule mj_objectArrayWithFilename:@"food_types.plist"]; 55 56 } 57 return _foodTypes; 58 } 59 60 - (void)viewDidLoad { 61 [super viewDidLoad]; 62 63 }
标签:
原文地址:http://www.cnblogs.com/yangshun-work/p/5041760.html