OCMapper icon indicating copy to clipboard operation
OCMapper copied to clipboard

Not mapping dictionary

Open MeGaPk opened this issue 8 years ago • 2 comments

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.

MeGaPk avatar Mar 04 '16 12:03 MeGaPk

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 avatar Mar 14 '16 15:03 aryaxt

@aryaxt Thanks :) Wait realization :+1:

MeGaPk avatar Mar 14 '16 21:03 MeGaPk