android的bean与iOS的model对比理解
2022-08-10 本文已影响0人
CoderZb
简单粗暴的理解(类比学习大法):android的bean等价于iOS的model。类中定义了属性,后续通过类取出属性中的值。
------------------------------------------android
------------------------------------------
在bean类中定义属性
StoreFocusBean.java
package com.mzy.one.bean;
public class StoreFocusBean {
private StoreBean store;
public StoreBean getStore() {
return store;
}
public static class StoreBean {
private Integer state;
public Integer getState() {
return state;
}
}
}
数据源获取并使用bean类
MineCollectActivity.java
import com.mzy.one.utils.GsonUtil;
import com.mzy.one.bean.StoreFocusBean;
public class MineCollectActivity extends AppCompatActivity implements View.OnClickListener {
public List<StoreFocusBean> mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine_collect);
// 省略部分代码
JSONArray jsonArray = jsonObject.optJSONArray("data");// jsonObject是接口获取到的列表数据
mList = GsonUtil.jsonToList(jsonArray.toString(), StoreFocusBean.class);
if (mList.get(position).getStore() != null) {
.....
}
}
------------------------------------------iOS
------------------------------------------
在model类中定义属性
ZBStoreTypeModel.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface ZBStoreTypeModel : FYBaseModel
@property(nonatomic,strong) NSString *categoryId;
@property(nonatomic,strong) NSString *state;
// 分类名
@property(nonatomic,strong) NSString *name;
@property(nonatomic,assign)NSInteger select;
@end
NS_ASSUME_NONNULL_END
ZBStoreTypeModel.m
#import "ZBStoreTypeModel.h"
@implementation ZBStoreTypeModel
+ (NSDictionary *)modelCustomPropertyMapper {
// 将personId映射到key为id的数据字段
return @{@"categoryId":@"id"};
}
@end
数据源获取并使用model类 (1)
#import "ZB_BenefitingPeopleVCViewController.h"
#import "ZBStoreTypeModel.h"
#import "ZB_BenefitCategoryCell.h"
@interface ZB_BenefitingPeopleVCViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray<ZBStoreTypeModel *> *storTypeDataArray;
@property (nonatomic,strong)NSString *categoryID;// 分类id
@end
@implementation ZB_BenefitingPeopleVCViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 省略部分代码
NSArray *temp = [NSArray yy_modelArrayWithClass:ZBStoreTypeModel.class json:responseObject[@"data"]];
[self.storTypeDataArray addObjectsFromArray:temp];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 省略部分代码
ZBStoreTypeModel *model = self.storTypeDataArray[indexPath.section];
model.select = 1;
self.categoryID = model.categoryId;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ZB_BenefitCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:ZB_BenefitCategoryCellID];
if (cell == nil) {
cell = [[ZB_BenefitCategoryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ZB_BenefitCategoryCellID];
}
// 执行(2)中的setModel方法
cell.model = self.storTypeDataArray[indexPath.section];
return cell;
}
@end
(2)
ZB_BenefitCategoryCell.h
#import <UIKit/UIKit.h>
#import "ZBStoreTypeModel.h"
NS_ASSUME_NONNULL_BEGIN
@interface ZB_BenefitCategoryCell : UITableViewCell
@property(nonatomic,strong)ZBStoreTypeModel *model;
@end
NS_ASSUME_NONNULL_END
ZB_BenefitCategoryCell.m
#import "ZB_BenefitCategoryCell.h"
@interface ZB_BenefitCategoryCell()
@property(nonatomic,strong)UILabel *nameLabel;
@end
@implementation ZB_BenefitCategoryCell
// 部分代码已省略
-(void)setModel:(ZBStoreTypeModel *)model{
_model = model;
if(model.select == 1){
self.nameLabel.textColor = [UIColor redColor];
}else{
self.nameLabel.textColor = [UIColor colorWithHexString:@"333333"];
}
self.nameLabel.text = model.name;
}
@end