字典轉模型

#import <Foundation/Foundation.h>atom

@interface CellStyleModel : NSObjectspa

@property (nonatomic, copy) NSString *type;.net

@property (nonatomic, copy) NSNumber * verCoord;3d

@property (nonatomic, copy) NSNumber * holCoord;orm

@property (nonatomic, copy) NSNumber * verSpan;ci

@property (nonatomic, copy) NSNumber * holSpan;get

- (id)initWithDict:(NSDictionary *)dict;it

+ (id)cellStyleModelWithDict:(NSDictionary *)dict;io

@endtable


#import "CellStyleModel.h"

@implementation CellStyleModel

- (id)initWithDict:(NSDictionary *)dict {

    if (self = [super init]) {

        [self setValuesForKeysWithDictionary:dict];

    }

    return self;

}

+ (id)cellStyleModelWithDict:(NSDictionary *)dict {

    return [[self alloc] initWithDict:dict];

}

@end


#import <Foundation/Foundation.h>

@interface CardStyleModel : NSObject

@property (nonatomic, copy) NSNumber * rowCount;

@property (nonatomic, copy) NSNumber * colCount;

// 注意這裏有模型嵌套

@property (nonatomic, strong) NSMutableArray *cells;

- (id)initWithDict:(NSDictionary *)dict;

+ (id)cardStyleModelWithDict:(NSDictionary *)dict;

@end


@implementation CardStyleModel

- (id)initWithDict:(NSDictionary *)dict {

    if (self = [super init]) {

        self.rowCount = dict[@"rowCount"];

        self.colCount = dict[@"colCount"];

        NSArray *dictcells = dict[@"cells"];

        NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dictcells.count];

        for (NSDictionary *dict in dictcells) {

            CellStyleModel *cellStyleModel = [CellStyleModel cellStyleModelWithDict:dict];

            [arrayM addObject:cellStyleModel];

        }

        self.cells = arrayM; 

    }

    return self;

}


+ (id)cardStyleModelWithDict:(NSDictionary *)dict {

    return [[self alloc] initWithDict:dict];

}

相關文章
相關標籤/搜索