首页 > 其他 > 详细

数据模型的构建及懒加载数据

时间:2016-02-13 21:53:51      阅读:312      评论:0      收藏:0      [点我收藏+]

1、数据模型的构建

技术分享

#import <Foundation/Foundation.h>

@interface AppModel : NSObject

@property (nonatomic, strong) NSString *icon;
@property (nonatomic, strong) NSString *name;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)appModelWithDict:(NSDictionary *)dict;

@end
#import "AppModel.h"

@implementation AppModel
- (instancetype)initWithDict:(NSDictionary *)dict {
  if (self = [super init]) {
    
    [self setValuesForKeysWithDictionary:dict];
  }
  
  return self;
}

+ (instancetype)appModelWithDict:(NSDictionary *)dict {
  return [[self alloc] initWithDict:dict];
}


@end

 

2、懒加载数据

@interface ViewController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ViewController
- (NSMutableArray *)dataArray { if (nil == _dataArray) { _dataArray = [NSMutableArray array]; //从plist中读取数据 NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]; NSArray *array = [NSArray arrayWithContentsOfFile:path]; //字典转模型 for (NSDictionary *dict in array) { AppModel *model = [AppModel appModelWithDict:dict]; [_dataArray addObject:model]; } } return _dataArray; } @end

PS:因为重写了getter方法,故调用时必须调用self.dataArray[]

 

数据模型的构建及懒加载数据

原文:http://www.cnblogs.com/codelu/p/5188300.html

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