加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

JSONModel的模型嵌套

发布时间:2020-12-16 18:46:38 所属栏目:百科 来源:网络整理
导读:JSONModel的模型嵌套 代码:https://github.com/SunshineTraveller/JSONModelDemo 我们解析数据的时候会遇到嵌套的数据,解析的时候会用到嵌套的模型,下面说下JSONModel嵌套的一些注意问题! 1.先简单说下JSONModel的简单用法 1.1单层模型 .h文件 #import J

JSONModel的模型嵌套

代码:https://github.com/SunshineTraveller/JSONModelDemo
我们解析数据的时候会遇到嵌套的数据,解析的时候会用到嵌套的模型,下面说下JSONModel嵌套的一些注意问题!
1.先简单说下JSONModel的简单用法
1.1单层模型
.h文件

#import <JSONModel/JSONModel.h>


@interface WX_FriendZonePeiTuModel :JSONModel


// 朋友圈配图

//单张

@property (nonatomic,copy)NSString *pic;

// 多张

strong)NSArray *data;

copy)NSString *orders;

copy)NSString *pid; // 后台为id,用pid代替

copy)NSString *name;

@end


.m文件


#import "WX_FriendZonePeiTuModel.h"


@implementation WX_FriendZonePeiTuModel

// pid代替id

+(JSONKeyMapper *)keyMapper {

return [[JSONKeyMapperalloc]initWithModelToJSONDictionary:@{

@"pid":@"id"

}];

}

// 所有的属性都为可选类型,

+(BOOL)propertyIsOptional:(NSString *)propertyName {


/* 可以设置某个属性为可选,下面的代码意为name为可选, 后台数据中没有name键

if ([propertyName isEqualToString:@"name"]) {

return YES;

}

*/

// 所有属性可选

returnYES;

}


@end



// 模型用法

WX_FriendZonePeiTuModel *model = [[WX_FriendZonePeiTuModelalloc]initWithDictionary:dicterror:nil];

dict为解析的字典

2.模型嵌套的使用
2.1建立模型
.h文件

#import <JSONModel/JSONModel.h>


// 内层模型

@interface WX_MoudleItemModel :JSONModel


@property (nonatomic,strong)NSArray *data;

copy)NSString *orders;

copy)NSString *addTime;

copy)NSString *content;

copy)NSString *fid;

copy)NSString *name;

copy)NSString *type;

strong)NSArray <NSDictionary *> *pics;

@end

// 外层模型

@protocolWX_MoudleItemModel; // 内层模型协议!!!!

@interface WX_MoudleSectionModel :JSONModel

copy)NSString *addTime;// section时间

@property (nonatomic,strong)NSArray <> *lists; // 数组元素为内层模型!!!

copy)NSString *rownum;


.m文件

#import "WX_MoudleSectionModel.h"


@implementation WX_MoudleItemModel


+(BOOL)propertyIsOptional:(NSString *)propertyName {

returnYES;

}


@end

@implementation WX_MoudleSectionModel


+(BOOL)propertyIsOptional:(NSString *)propertyName {

returnYES;

}


@end




2.2 cell文件

在cell的.h文件里,直接引入外层模型.h文件

#import "WX_FriendMutipleView.h"

#import <UIKit/UIKit.h>

@class WX_FriendZonePeiTuModel;

@interface WX_FriendZonePeiTuCell :UICollectionViewCell


/** weitu */

strong)UIImageView *peiTuImV;

/** content */

strong)UILabel *content;

/** model */

strong)WX_FriendZonePeiTuModel *model;

// 内层模型为collectionView的cell(外层模型为sectionHeader,写在reusebleView里)

strong)WX_MoudleItemModel *item;

strong)WX_FriendMutipleView *mutiView;



cell 的.m文件里

-(void)setItem:(WX_MoudleItemModel *)item {

/*

你的代码...

*/

}

2.3 viewController文件

.m文件

#import "WX_MyFriendZoneViewController.h"

#import "WX_MoudleSectionModel.h" // 引入外层模型.h文件

@class WX_MoudleItemModel; // 内层模型


模型解析

-(void)xxx {

if (data.count) {

for (NSDictionary *modelDictin data) {

WX_MoudleSectionModel *model = [[WX_MoudleSectionModelalloc]initWithDictionary:modelDicterror:nil];

[weakSelf.peiTuArraddObject:model];

}

}

}


cell赋值

- (__kindofUICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

WX_MoudleSectionModel *model =self.peiTuArr[indexPath.section]; // 外层

WX_MoudleItemModel *items = model.lists[indexPath.item]; // 内层

WX_FriendZonePeiTuCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:peiTuCellforIndexPath:indexPath];

cell.item = items; // cell赋值

return cell;

}


大功告成


我遇到的坑:

JSONModel 模型嵌套
Cell中声明的时候,不要@class ,要#import外层模型.h文件 ,并且在ViewController中引入外层模型的.h + @class 内层模型;
不然会初现
-[__NSDictionaryM content]: unrecognized selector sent to instance xxxx

千万注意:模型.m文件里别写错了
+(BOOL)propertyIsOptional:(NSString
*)propertyName {
return YES;
}
而不是
+(
BOOL)propertyIsIgnored:(NSString
*)propertyName {
return YES;
}
我因为疏忽导致外层模型死活赋值失败,属性全为nil;因为第二个方法是忽略了所有属性(模型的所有属性都是ignore忽略的),第一个是所有属性为可选类型(模型的属性数目可以比接口数据字典的键数目多);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读