2016-08-26 3 views
0

я моделируется мой результат апи следующим образом:JsonModel не может преобразовать массив в JSON для jsonmodel унаследовал класс

#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 

И я называю апи с AFNetworking вот так:

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 также преобразует массив в JSONObject, если вы хотите получить доступ к свойствам класса 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 "данные" из сети Response

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

найти общее количество PTPDestination и запустить в цикле. Вы можете получить доступ к объекту, подобному этому.

PTPDestination *ptpdest = [ptpInd PTPDestination[0]]; 
Смежные вопросы