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