OCMapper
OCMapper copied to clipboard
Not mapping dictionary
Hello! Maybe its bug, i wanna report it.
So, we have Class:
@interface TFResponse : NSObject
@property(nonatomic, assign) BOOL status;
@property(nonatomic, weak) NSDictionary *data;
@end
And DICT:
{
data = {
code = 0;
message = "test message";
};
status = 0;
}
After use code:
TFResponse *response = [TFResponse objectFromDictionary:responseObject];
NSLog(@"API: TFResponse status: %d", response.status);
NSLog(@"API: TFResponse data: %@", response.data);
// response.data = responseObject[@"data"];
We have result:
API: TFResponse status: 0
API: TFResponse data: {
}
For fix that, i use:
response.data = responseObject[@"data"];
But maybe it is a bug, and so I decided to report it.
Wait answer. Best regards, Ivan.
Hi, unfortunately the library doesn't map dictionaries automatically. However you could write a transformer.
Basically this code intercepts mapping for that key, and calls the block, so you have the opportunity to provide a value to be mapped to the property, and in this case you want to map the exact value without any change, so you simply return currentNode.
[mappingProvider mapFromDictionaryKey:@"data" toPropertyKey:@"data" forClass:[TFResponse class] withTransformer:^id(id currentNode, id parentNode) {
return currentNode;
}];
Will add this as an enhancement, thanks
@aryaxt Thanks :) Wait realization :+1: