Closed pechn closed 8 years ago
if you want to have only AnotherPost
in the array
[inCodeMappingProvider mapFromDictionaryKey:@"posts" toPropertyKey:@"posts" withObjectType:[AnotherPost class] forClass:[Author class]];
If you want to have both Post
& AnotherPost
in the array, you can use a transformer for a given key ("post" in this case) and manually map each object the way you want.
[mappingProvider mapFromDictionaryKey:@"posts" toPropertyKey:@"posts" forClass:[Author class] withTransformer:^id(id currentNode, id parentNode) {
NSMutableArray *differentPosts = [NSMutableArray array];
for (NSDictionary *dict in currentNode) {
NSString *type = dict[@"type"];
if ([type isEqualToString:@"post") { //logic to differentiate posts
[differentPosts addObject:[Post objectFromDictionary:dict]];
}
else if ([type isEqualToString:@"another-post") { {
[differentPosts addObject:[AnotherPost objectFromDictionary:dict]];
}
}
return differentPosts;
}];
Let me know if this works
@aryaxt Yes, it works well! Thank you very much:)
Dears, Suppose I have the objects below:
And the dictionary data like:
The statement below will deserialize Author object using Post class for parsing the array data.
Author *author = [Author objectFromDictionary:aDictionary];
My question is how can I configure AnotherPost class for parsing the array data instead of Post class?
It would be appreciate for you to give me some guides.
Thank you:)