美文网首页iOS Developer
ios 模型对象创建

ios 模型对象创建

作者: Bryan5137 | 来源:发表于2016-05-20 13:09 被阅读502次

    创建一个类,继承NSObject

    .h文件

    #import <Foundation/Foundation.h>

    @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 模型对象创建

        本文链接:https://www.haomeiwen.com/subject/npknrttx.html