IOS开发中model的简单使用

来源:互联网 发布:淘宝卖家好评回复模板 编辑:程序博客网 时间:2024/06/05 03:07

 [super viewDidLoad];//   初始化赋值model 方法一    self.view.backgroundColor = [UIColor colorWithRed:0.944 green:0.925 blue:1.000 alpha:1.000];    //原始数据    NSArray *list = @[@{@"name":@"小明", @"age":@(18), @"height":@(18.5)}];    //获取元数据的第一条数据 接收转换好的数据模型    NSMutableArray *userList = [NSMutableArray array];        //把获取到的数据转换成model    //遍历原始数据,原始数据里面存的是字典,用字典去接收 (info)    for (NSDictionary *info in list) {        //使用model  可以放在拆包数据时 写错而无法读取数据,        Model *model = [[Model alloc]init];        model.name =info[@"name"];        //把获取到的内容转换成对应的格式        model.age = [info[@"age"] integerValue];        model.height = [info[@"height"] integerValue];        //把转换好的model添加到以后一要使用的数组里面        [userList addObject:model];    }    Model *one = userList[0];    NSLog(@"%@",one.name);    //方法二 利用自定义构造方法     NSArray *list2 = @[@{@"name":@"小明", @"age":@(18), @"height":@(18.5)}];    //获取元数据的第一条数据 接收转换好的数据模型    NSMutableArray *userList = [NSMutableArray array];        for (NSDictionary *info in list2) {        Model *mode = [[Model alloc]initWithUserInfo:info];        [userList addObject:mode];    }

利用model可以将数据整理模块化




0 0