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

延迟加载的固定写法

时间:2015-05-18 20:23:51      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

 1 @property(nonatomic,strong) NSArray *questions;
 2 
 3 -(NSArray *)questions
 4 
 5 {
 6 
 7     if(_questions==nil)
 8 
 9     {
10 
11         //1 加载plist文件
12 
13         NSArray *options=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"]];
14 
15         NSMutableArray *questionArray=[NSMutableArray array];
16 
17         //2 字典转模型
18 
19         for(NSDictionary *dict in options)
20 
21         {
22 
23             //遍历每一个字典,转模型
24 
25             //2.1 创建模型对象 调用自定义的类方法,传入字典参数,在类方法内部进行字典转模型的操作
26 
27             HYQuestions *question=[HYQuestions questionsWithDict:dict];
28 
29             //2.2 把模型对象加到新的数组中
30 
31             [questionArray addObject:question];
32 
33         }
34 
35         
36 
37         //3.赋值
38 
39         _questions=questionArray;
40 
41     }
42 
43     return _questions;
44 
45 }
46 
47  
48 
49  
50 
51 @implementation HYQuestions
52 
53 -(instancetype)initWithDict:(NSDictionary *)dict
54 
55 {
56 
57     if(self=[super init])
58 
59     {
60 
61         self.icon=dict[@"icon"];
62 
63         self.title=dict[@"title"];
64 
65         self.answer=dict[@"answer"];
66 
67         self.options=dict[@"options"];
68 
69     }
70 
71     return self;
72 
73 }
74 
75 +(instancetype)questionsWithDict:(NSDictionary *)dict
76 
77 {
78 
79     return  [[self alloc]initWithDict:dict];
80 
81 }
82 
83 @end

 

延迟加载的固定写法

标签:

原文地址:http://www.cnblogs.com/shy1015/p/4512937.html

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