ios – JSONModel Bad属性协议声明?

ios – JSONModel Bad属性协议声明?,第1张

概述我在 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实例.

瞧!

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1011113.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存