4

JSONModel ライブラリによる 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.'

何か助けてください。

4

2 に答える 2

4

貼り付けたコードに2つの問題があります。

あなたのモデルは良いですが、それは1つのアイテムのモデルです-つまり。これは、一度にすべての画像をロードするのではなく、単一の画像をロードするために使用するモデルです。したがって、画像のコレクションがあることを説明するモデルと、各画像オブジェクトを説明する別のモデル(所有しているモデル)が必要です。

2番目の問題は、プロパティの1つが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文字列を読み取り、画像モデルを作成します。

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

次に、imagesCollection.imagesの各要素がImageModelインスタンスになります。

出来上がり!

于 2013-03-12T17:43:24.353 に答える