码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-02-13 21:54:18      阅读:290      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!