zoukankan      html  css  js  c++  java
  • 数据模型的构建及懒加载数据

    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[]

  • 相关阅读:
    (HDU)1097 --A hard puzzle(难题)
    (HDU)1096 --A+B for Input-Output Practice (VIII)(输入输出练习(VIII))
    PAT B1008——数组元素循环右移
    test
    vue iconfont矢量图
    css3简单旋转
    vue 路由的安装及使用
    vue 父组件与子组件之间的相互调用
    vue 脚手架安装
    PHP 加密方式
  • 原文地址:https://www.cnblogs.com/codelu/p/5188300.html
Copyright © 2011-2022 走看看