重写set方法及懒加载

1、先判断数组是否为空,为空再说明已经加载过,不为空则未加载

- (NSArray *) questions

{

    if (_questions == nil) {

        

        //1.加载plist

        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil]];

        

        

        //2.字典转模型

        NSMutableArray *questionArray = [NSMutableArray array];

    

        for (NSDictionary *dict in dictArray) {

            

            WNHQuestion *question = [WNHQuestion questionWithDict:dict];

            [questionArray addObject:question];

        }

        

        //3.赋值

        _questions = questionArray;

    }

    return _questions;

}

原文地址:https://www.cnblogs.com/loserof/p/4082583.html