2016-08-26 1 views
0

をjsonmodelするJSONの配列を変換することはできません。JsonModel私は、次のように私のAPI結果をモデル化した継承クラス

#import "PTPDestination.h" 
@interface PTPIndex : PTPBaseEntity 
@property (strong, nonatomic) NSNumber * citiesCount; 
@property (strong, nonatomic) NSNumber * hotelsCount; 
@property (strong, nonatomic) NSArray<PTPDestination *> * top_destinations; 
@end 

私もこのようPTPDestinationをモデル化:

@interface PTPDestination : PTPBaseEntity 
@property (assign, nonatomic) NSNumber * id; 
@property (assign, nonatomic) NSNumber * min_price; 
@property (assign, nonatomic) NSNumber * max_discount; 
@property (assign, nonatomic) NSString * title; 
@end 

そして、私は私のAPIを呼び出しますAFNetworking like:

AFHTTPSessionManager * manager = [self createAPISessionManager]; 
[manager GET:[self createServiceUrlWithMethod:@"someURL"] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) { 
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 
    NSError * error = nil; 
    PTPIndex * index = [[PTPIndex alloc] initWithDictionary:responseObject error:&error]; 
    if (error) { 
     callback (nil, [PTPApiCenteralizedErrorHandler getErrorFromApiError:error task:task responseObject:responseObject]); 
     return; 
    } 
    callback (index, nil); 
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 
    callback (nil, [PTPApiCenteralizedErrorHandler getErrorFromApiError:error task:task]); 
}]; 

問題は宛先の配列にあります。配列がPTPDestinationオブジェクトに変換されず、NSDictionariesの配列として残っている理由はわかりません。

なぜこのようなことが起こり、カスタムクラスの配列を持つことができますか?

+0

サーバーからJSONペイロードが戻ってくる例がありますか? –

+0

@CraigOtis私はこのurl ==> www.pintapin.com/service/index/mobileと呼んでいます。 – aakpro

+0

'PTPBaseEntity'はどうなっていますか? 'JSONModel'も拡張していますか? –

答えて

0

いいえ、JSONモデルまた、PTPDestinationクラスのプロパティにアクセスする場合は、配列をJSONObjectに変換します。ネットワーク応答

PTPIndex *ptpInd = [[PTPIndex alloc] initWithDictionary:data error:&error]; 

PTPDestinationの総数を見つけて、ループ内で実行さから

PTPIndexクラス

#import "JSONModel.h" 
@interface PTPIndex : JSONModel 
@property (strong, nonatomic) NSNumber * citiesCount; 
@property (strong, nonatomic) NSNumber * hotelsCount; 
@property (strong, nonatomic) NSArray<PTPDestination *> * top_destinations; 
@end 

PPTPDestinationクラス

#import "JSONModel.h" 
@interface PTPDestination : JSONModel 
@property (assign, nonatomic) NSNumber * id; 
@property (assign, nonatomic) NSNumber * min_price; 
@property (assign, nonatomic) NSNumber * max_discount; 
@property (assign, nonatomic) NSString * title; 
@end 

NSDictionaryの "データ"。 このようにオブジェクトにアクセスできます。

PTPDestination *ptpdest = [ptpInd PTPDestination[0]]; 
関連する問題