首页 > 其他 > 详细

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

时间:2015-12-04 14:30:26      阅读:351      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!