zoukankan      html  css  js  c++  java
  • 字典数组转为模型数组

    在控制器上懒加载

    - (NSArray *)statuses
    {
        if (_statuses == nil) {
            // 加载plist中的字典数组
            NSString *path = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];
            NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
            
            // 字典数组 -> 模型数组
            NSMutableArray *statusArray = [NSMutableArray array];
            for (NSDictionary *dict in dictArray) {
                XSPStatus *status = [XSPStatus statusWithDict:dict];
                [statusArray addObject:status];
            }
            
            _statuses = statusArray;
        }
        return _statuses;
    }

    XSPStatus.h文件里

    #import <Foundation/Foundation.h>
    
    @interface XSPStatus : NSObject
    @property (strong, nonatomic) NSString *name;
    @property (strong, nonatomic) NSString *text;
    @property (strong, nonatomic) NSString *icon;
    @property (strong, nonatomic) NSString *picture;
    @property (assign, nonatomic, getter=isVip) BOOL vip;
    
    + (instancetype)statusWithDict:(NSDictionary *)dict;
    @end

    XSPStatus.m文件里

    #import "XSPStatus.h"
    
    @implementation XSPStatus
    + (instancetype)statusWithDict:(NSDictionary *)dict
    {
        XSPStatus *status = [[self alloc] init];
        [status setValuesForKeysWithDictionary:dict];
        return status;
    }
    @end

    取出数据,结果如下:

    XSPStatus *a = self.statuses[indexPath.row];
    NSLog(@"%@--%@--%@--%@",a.buyCount,a.price,a.title,a.icon);

     

  • 相关阅读:
    嘀嘀咕 (1)
    碎碎念(4)
    渲染层错误] TypeError: Cannot read property 'replace' of undefined at rewrit
    怎么跳出foreach
    vs code的Go Live不出现
    ES6
    h5分享到微信,分享到朋友圈
    网页之间传值与获取值
    原生js添加节点的高级简便写法
    原型链
  • 原文地址:https://www.cnblogs.com/xsphehe/p/5622503.html
Copyright © 2011-2022 走看看