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

Not mapping dictionary #43

Open MeGaPk opened 8 years ago

MeGaPk commented 8 years ago

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.

aryaxt commented 8 years ago

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

MeGaPk commented 8 years ago

@aryaxt Thanks :) Wait realization :+1: