首页 > 其他 > 详细

字典转模型和懒加载

时间:2016-06-01 01:25:42      阅读:203      评论:0      收藏:0      [点我收藏+]

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;

}

字典转模型和懒加载

原文:http://www.cnblogs.com/PJXWang/p/5548159.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!