延迟加载的固定写法

 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

 

posted on 2015-05-18 19:57  我是一只小蜜蜂  阅读(196)  评论(0编辑  收藏  举报

导航