aryaxt / OCMapper

Objective-C library to easily map NSDictionary to model objects, works perfectly with Alamofire. ObjectMapper works similar to GSON
MIT License
347 stars 45 forks source link

Nested mapping of classes #37

Closed sssuourabh closed 9 years ago

sssuourabh commented 9 years ago

I have json which contains an array of other classes as well so how to do nested mapping between classes. Example @interface APIOrganization : NSObject

@property (nonatomic , strong) NSString id; @property (nonatomic , strong) NSString name; @property (nonatomic , strong) APIPhoto rmcPhoto; @property (nonatomic , strong) NSMutableArray Service; @property (nonatomic , strong) NSMutableArray *osd; @end

Here the APIOrganization class i am using

APIOrganization *ApiOrg = [[ObjectMapper sharedInstance] objectFromSource:orgDict toInstanceOfClass:[APIOrganization class]]; Service is array of class Service class but i am not getting the array which contain Service

Please guide me its very urgent @interface APIService : NSObject @property (nonatomic , strong) NSString *id;// Doubt

@property (nonatomic , strong) NSString *name;

@property (nonatomic , strong) NSString *descriptions;

@property (nonatomic , strong) NSString *updatedOn;

aryaxt commented 9 years ago

automatic array mapping happens when dictionary key is the same as property name and the class name, so you could do 2 things:

1- Name the property apiServices or apiService where the dictionary key would be the same

OR

2- write mapping:

InCodeMappingProvider *mappingProvider = [[InCodeMappingProvider alloc] init];

[mappingProvider mapFromDictionaryKey:@"WHATEVER_THE_KEY_IS" toPropertyKey:@"Service" withObjectType:APIService.class forClass:APIOrganization.class];
// Add any other mapping needed

[[ObjectMapper sharedInstance] setMappingProvider:mappingProvider];