iOS   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – JSONModel Bad属性协议声明?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我在 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"},{"i
我在 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实例.

瞧!

@H_607_63@

大佬总结

以上是大佬教程为你收集整理的ios – JSONModel Bad属性协议声明?全部内容,希望文章能够帮你解决ios – JSONModel Bad属性协议声明?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。