延遲加載的固定寫法

 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
相關文章
相關標籤/搜索