码迷,mamicode.com
首页 > 其他好文 > 详细

字典转模型的三种方法之二:KVC

时间:2015-12-04 14:32:25      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

 

 1 -(NSArray *)tgs
 2 {
 3     NSString *path = [[NSBundle mainBundle]pathForResource:@"tgs" ofType:@"plist"];
 4     NSMutableArray *tgsArray = [NSMutableArray array];
 5     NSArray *tgsDicts = [NSArray arrayWithContentsOfFile:path];
 6     for (NSDictionary *dict in tgsDicts) {
 7         tg *t = [tg tgWithDict:dict];
 8         [tgsArray addObject:t];
 9     }
10     
11     _tgs = tgsArray;
12     return _tgs;
13 }
 1 //#import <Foundation/Foundation.h>
 2 //
 3 //@interface tg : NSObject
 4 //@property (nonatomic, copy) NSString *icon;
 5 //@property (nonatomic, copy) NSString *title;
 6 //@property (nonatomic, copy) NSString *price;
 7 //@property (nonatomic, copy) NSString *buyCount;
 8 //
 9 //-(instancetype)initWithDict:(NSDictionary *)dict;
10 //+(instancetype)tgWithDict:(NSDictionary *)dict;
11 //@end
12 
13 #import "tg.h"
14 
15 @implementation tg
16 -(instancetype)initWithDict:(NSDictionary *)dict
17 {
18     if (self = [super init]) {
19         [self setValuesForKeysWithDictionary:dict];//KVC
20     }
21     return self;
22 }
23 +(instancetype)tgWithDict:(NSDictionary *)dict
24 {
25     return [[self alloc]initWithDict:dict];
26 }
27 @end

 

字典转模型的三种方法之二:KVC

标签:

原文地址:http://www.cnblogs.com/yangshun-work/p/5018952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!