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

     

  • 相关阅读:
    [C语言
    [C语言
    [C语言
    [C语言
    [C语言
    [C语言
    [iOS]超详细Apache服务器的配置(10.10系统)
    IOS优秀博客
    「C」 数组、字符串、指针
    103.Binary Tree Zigzag Level Order Traversal(层序遍历)
  • 原文地址:https://www.cnblogs.com/xsphehe/p/5622503.html
Copyright © 2011-2022 走看看