字典转模型和懒加载

1.字典转模型

创建一个类,继承自NSObject,属性名和字典的键一致

可以实现字典转模型

@implementation TZMessage

+(instancetype)messageWithDict(NSDictioary*)dict{

  TZMessage *mode = [[TZMessage alloc] init];

  [mode setValuesForKeysWithDictonary:dict];

  return mode;

}

2.懒加载

+(NSArray *)messages{

  if(!_messages){

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil];

    NSArray *array = [NSArray arrayWithContentsOfFile:filePath];

    NSMutableArray *tempArray = [NSMutableArray array];

    for(NSDictionary *dict  in array){

      TZMessage *mode = [ TZMessage messageWithDict:dict];

      [tempArray  addObject:mode]

    }

    _messages = tempArray;

  }

  retrun  _messages;

}

原文地址:https://www.cnblogs.com/PJXWang/p/5548159.html