Search code examples
iosgithub-mantle

Mantle - MTLJSONSerializing two value for the same propertie


I call an api which can have differents interface for the same value "id" : "id", "_id" or "pId".

But currently, it's works for only the first : @"id": @"_id". the other are ignored.

JSON

 object:{ "pId" : 192039,
         "name" : "test}

IOS WHICH WORKS: self.id = 192039

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"id": @"pId",
             @"id": @"_id",
             @"id": @"id",
             @"title": @"name",
            };
}

IOS WHICH NOT WORKS: self.id =

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{

             @"id": @"_id",
             @"id": @"id",
             @"id": @"pId",
             @"title": @"name",
            };
}

EDIT

Keys in a dictionary must be unique....

The only solution I found is to create 3 differents properties and override setters like that:

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"pId": @"pId",
             @"id2": @"_id",
             @"id": @"id",
             @"title": @"name",
            };
}

- (void) setPid:(NSString *)pId
{
    _id = pId;
}

- (void) setId2:(NSString *)id2
{
    _id = id2;
}

Solution

  • Keys in a dictionary must be unique....

    The only solution I found is to create 3 differents properties and override setters like that:

    + (NSDictionary *)JSONKeyPathsByPropertyKey {
        return @{
                 @"pId": @"pId",
                 @"id2": @"_id",
                 @"id": @"id",
                 @"title": @"name",
                };
    }
    
    - (void) setPid:(NSString *)pId
    {
        _id = pId;
    }
    
    - (void) setId2:(NSString *)id2
    {
        _id = id2;
    }