JSONModel Bad属性协议声明?

时间:2013-02-14 22:00:59

标签: ios xcode json jsonmodel

我在按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.'

请帮忙吗?

2 个答案:

答案 0 :(得分:4)

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

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

第二个问题是你的一个属性是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* name;
@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 实例。

瞧!

答案 1 :(得分:0)

我认为您没有在.m文件中实现此类。因此,当JsonModel内部执行NSClassFromString()方法时,它将崩溃。

相关问题