ios 模型对象创建

创建一个类,继承NSObject

.h文件

#import

@interface TestModel :NSObject

@property(nonatomic,copy)NSString*icon;

@property(nonatomic,copy)NSString*name;

-(instancetype)initWithDic:(NSDictionary* )dict;

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

@end

.m文件

#import "TestModel.h"

@implementation TestModel

-(instancetype)initWithDic:(NSDictionary* )dict{

if(self= [super init]) {

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

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

}

return self;

}

+(instancetype)modelWithDic:(NSDictionary* )dict{

return [[self alloc] initWithDic:dict];

}

@end

RAC使用:

#import "ReactiveCocoa/ReactiveCocoa.h"


NSArray* dicArray = @[@{@"icon":@"1",@"name":@"aa"},@{@"icon":@"2",@"name":@"bb"},

@{@"icon":@"3",@"name":@"cc"},@{@"icon":@"4",@"name":@"dd"}];

NSMutableArray*modelArray = [NSMutableArray array];

[dicArray.rac_sequence.signal subscribeNext:^(id x) {

TestModel* item = [TestModel modelWithDic:x];

[modelArray addObject:item];

}];

你可能感兴趣的:(ios 模型对象创建)