数据赋值

1 在网络请求中,获得的数据对控件进行赋值时,一定在数据的set方法中赋值,否则极易造成赋值失败,导致控件的相应地数据为空。当数据是通过网络请求得到的,赋值时,出现控件的相应数据属性为空时,很有可能是赋值时,控件还没初始化创建出来,导致赋值(=)的右侧有值,而= 的左侧为空(即控件的数据属性为空)。

2 在使用数据模型时,诸如大的模型中有一个用数组存放的字典小模型时,即存在嵌套模型时,由于在对大的模型(即小模型的上一级模型赋值时,已经对小模型进行了初始化小模型,因此在控制器或view中不能再次初始化小模型,否则将导致数据错误,导致小模型数据为空,界面无数据展示。记住,一旦模型在模型类中已经初始化,那么在控制器或view中一定不能再次赋值初始化。经典案例如下:

#import "HRBaseModel.h"

@interface ActivityRecommentModel : HRBaseModel

@property (nonatomic,assign) NSInteger ArticleID;    //文章ID

@property (nonatomic,copy) NSString *Title;    //文章标题

@property (nonatomic,copy) NSString *Image;    //文章图片URL

@property (nonatomic,copy) NSString *Products;//关联商品

@property (nonatomic,strong) NSArray *ProductList;//关联商品的LIST

@end

 

#import "ActivityRecommentModel.h"

#import "ActivityRecDetProductModel.h"

 

@implementation ActivityRecommentModel

 

- (instancetype)initWithDictionary:(NSDictionary *)dictionary

{

    self = [super initWithDictionary:dictionary];

    if (self) {

        [self setValuesForKeysWithDictionary:dictionary];

        NSArray *productList = dictionary[@"ProductList"];

        NSMutableArray *muArr = [NSMutableArray array];

        for (NSDictionary *dict in productList) {

//在模型中已经初始化了ActivityRecDetProductModel,因此在控制器或view中一定不能再次初始化,否则错误,模型初始化只能进行一次

            ActivityRecDetProductModel *productModel = [[ActivityRecDetProductModel alloc] initWithDictionary:dict];

            [muArr addObject:productModel];

        }

        _ProductList = muArr;

    }

    return self;

}

 

@end

 

转载于:https://my.oschina.net/llfk/blog/785613

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值