iOS新手学习

iOS数据解析

2018-06-15  本文已影响130人  myk
#import <Foundation/Foundation.h>

@interface ModelLabel : NSObject

@property (nonatomic, copy) NSString *content;
@property (nonatomic, copy) NSString *fontcolor;
@property (nonatomic, copy) NSString *bgcolor;

+(instancetype)modelWithDict:(NSDictionary *)dict;

@end
#import "ModelLabel.h"

@implementation ModelLabel

+(instancetype)modelWithDict:(NSDictionary *)dict{
    return [[self alloc] initWithDict:dict];
}

-(instancetype)initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

@end

.h
#import <Foundation/Foundation.h>
#import "ModelLabel.h"

@interface ModelList : NSObject

@property (nonatomic, copy) NSString *time_waitpost;
@property (nonatomic, copy) NSString *bizname;
@property (nonatomic, copy) NSString *bizaddress;
@property (nonatomic, copy) NSString *receiver_name;
@property (nonatomic, copy) NSString *receiver_address;
@property (nonatomic, copy) NSArray <ModelLabel *>*arrLabel;

+(instancetype)modelWithDict:(NSDictionary *)dict;

@end
.m
#import "ModelList.h"
@implementation ModelList

+(instancetype)modelWithDict:(NSDictionary *)dict{
    return [[self alloc] initWithDict:dict];
}

-(instancetype)initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
        NSMutableArray *arr = [NSMutableArray array];
        //取出Labels数组里面的数据转换成ModelLabel模型,如果是模型里面嵌套有其他模型,需要在这里调用其他模型的转模型方法
        NSArray *arrTemp = dict[@"Labels"];
        for (NSDictionary *dictTemp in arrTemp) {
            [arr addObject:[ModelLabel modelWithDict:dictTemp]];
        }
        self.arrLabel = [arr copy];
    }
    return self;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

@end
上一篇 下一篇

猜你喜欢

热点阅读