3

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;
}
4

2 回答 2

0

你不能重复 te 键

@{key:value,...}

你可以试试这样的其他东西

return @{
     @"id" : @[@"_id",@"id",@"pId"],
     @"title" : @"name"
   };

或为@"id" 键添加前缀,例如@"id1

于 2014-03-07T23:02:30.033 回答
0

字典中的键必须是唯一的......

我找到的唯一解决方案是创建 3 个不同的属性并覆盖这样的设置器:

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

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

- (void) setId2:(NSString *)id2
{
    _id = id2;
}
于 2014-03-14T17:34:30.170 回答