0

I have modeled my api result as following:

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

I also modeled PTPDestination like this:

@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

And I call my api with AFNetworking like this:

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]);
}];

The problem is with array of destinations. I don't know why the array is not converted to PTPDestination object and it remains as an array of NSDictionaries.

Why this happens and how can I have an array of my custom class?

aakpro
  • 1,538
  • 2
  • 19
  • 53

1 Answers1

0

No, JSON Model Also Converted the Array to JSONObject, if you want to access the PTPDestination class properties.

PTPIndex class

#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 class

#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 "data" from the network Response

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

find the total number of PTPDestination and run in the loop. You can access the object like this.

PTPDestination *ptpdest = [ptpInd PTPDestination[0]];