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

ios – JSONModel Bad属性协议声明?

发布时间:2020-12-14 17:38:26 所属栏目:百科 来源:网络整理
导读:我在 JSONModel library读JSON时遇到问题 {"images":[{"id":1,"name":"name1","img":"3423","note":"note1"},{"id":2,"name":"name2","img":"rew","note":"note2"},{"id":3,"name":"name3","img":"dsfs","note":"note3"},{"id":4,"name":"name4","img":"cxvx
我在 JSONModel library读JSON时遇到问题

{"images":[{"id":1,"name":"name1","img":"3423","note":"note1"},{"id":2,"name":"name2","img":"rew","note":"note2"},{"id":3,"name":"name3","img":"dsfs","note":"note3"},{"id":4,"name":"name4","img":"cxvxc","note":"note4"},{"id":5,"name":"name5","img":"erwe","note":"note5"}]}

班级模型是

#import "JSONModel.h"

@protocol ImagesModel @end

@interface ImagesModel : JSONModel
@property int id;
@property (strong,nonatomic) NSString* name;
@property (strong,nonatomic) UIImage* img;
@property (strong,nonatomic) NSString* note;
@end

我收到了这个错误

Terminating app due to uncaught exception 'Bad property protocol declaration',reason: '<ImagesModel> is not allowed JSONModel property protocol,and not a JSONModel class.'

有什么帮助吗?

解决方法

我可以看到您粘贴的代码存在两个问题.

你的模型很好,但它是一个项目的模型 – 即.它是您将用于加载单个图像的模型 – 不是所有图像一次加载.因此,您需要一个模型来描述您拥有一组图像,以及另一个模型(您拥有的模型)来描述每个图像对象.

第二个问题是你的一个属性是UIImage对象,但你在JSON提要中传递字符串.

因此,要让您的示例正常工作,您需要:

#import "JSONModel.h"

//define the single image object protocol
@protocol ImageModel @end

//define the single image model
@interface ImageModel : JSONModel
@property int id;
@property (strong,nonatomic) NSString* img;
@property (strong,nonatomic) NSString* note;
@end

@implementation ImageModel
@end

//define the top-level model for the collection of images
@interface Images : JSONModel
@property (strong,nonatomic) NSArray<ImageModel>* images;
@end

然后读取您的JSON字符串并创建一个Images模型:

NSError* err = nil;
Images* imagesCollection = [[Images alloc] initWithString:JSONstring error:&err];

然后imagesCollection.images中的每个元素都将是一个ImageModel实例.

瞧!

(编辑:李大同)

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

    推荐文章
      热点阅读