字典数组转为模型数组

Posted 低头捡到蛋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字典数组转为模型数组相关的知识,希望对你有一定的参考价值。

在控制器上懒加载

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

 

 

以上是关于字典数组转为模型数组的主要内容,如果未能解决你的问题,请参考以下文章

怎么将数组转为json

MJExtension使用

python 数组 变成 字典的方法

字典模型

MJExtension的使用

将字典中的数组转换为数组列表