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);

     

  • 相关阅读:
    pip错误:'utf-8' codec can't decode byte解决方法
    windows中python2与python3共存
    Alpha 冲刺 (3/10)
    Alpha 冲刺 (2/10)
    Alpha 冲刺 (1/10)
    项目需求分析报告答辩总结
    项目选题报告答辩总结
    项目UML设计(团队)
    # 第七次作业--项目需求分析(团队)
    结对项目--第二次作业
  • 原文地址:https://www.cnblogs.com/xsphehe/p/5622503.html
Copyright © 2011-2022 走看看