【问题标题】:RestKit mapping with parent key as attribute and key contains parenthesis以父键为属性且键包含括号的 RestKit 映射
【发布时间】:2018-03-31 19:29:00
【问题描述】:

上下文:

// JSON
"Name_Field" : {
    "param_1":"value_1",
    "param_2":"value_2"
}

// Class
class Field {
    name
    param1
    param2
}

// Mapping Functionality
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"name"];
[mapping addAttributeMappingsFromDictionary:@{
    @"(name).param_1":@"param1",
    @"(name).param_2":@"param2"
}];

问题:

我目前正在使用上述 JSON/类/映射代码。我已经使用了一段时间,一切都按预期工作。

今天我遇到了 JSON 中的键包含括号并导致映射失败的情况。有没有办法让它工作?

谢谢!

示例:

"Name (haha this will break)" : {
    "param_1":"value_1",
    "param_2":"value_2"
}

【问题讨论】:

  • 您是否遇到任何错误?或者在映射发生时该字段被简单地忽略?
  • 我没有收到错误消息。自从发布这个问题以来,我一直在调试,看起来括号被替换的方式与用于标记名称的方式相同。即“名字{哈哈这将打破}”。
  • 经过更多调试后,我会在这里尝试您的解决方案。
  • 我发现它用括号替换了括号。在<RK_NESTING_ATTRIBUTE> 到“name”属性的初始映射之后,它会尝试在“{name}.param_1”的定义映射中更新“{name}”。当它更新这些映射时,它会创建一个新的RKPropertyMapping 对象并将其设置为调用RKStringByReplacingUnderscoresWithBraces 的sourceKeyPath,这会替换括号。

标签: ios objective-c restkit


【解决方案1】:

我认为RestKit 会感到困惑,因为它不知道映射时使用哪个括号。所以我的猜测是用大括号替换它们:

[mapping addAttributeMappingsFromDictionary:@{
    @"{name}.param_1":@"param1",
    @"{name}.param_2":@"param2"
}];

让我知道这是否有效。

【讨论】:

    【解决方案2】:

    解决方案:

    更新RKPropertyMapping 中的RKStringByReplacingUnderscoresWithBraces 方法,不要用大括号替换括号,然后确保在属性映射中使用大括号。

    // RKPropertyMapping
    static NSString *RKStringByReplacingUnderscoresWithBraces(NSString *string)
    {
        return string;
        //return [[string stringByReplacingOccurrencesOfString:@"(" withString:@"{"] stringByReplacingOccurrencesOfString:@")" withString:@"}"];
    }
    
    
    // Attribute Mappings
    [mapping addAttributeMappingsFromDictionary:@{
        @"{name}.param_1":@"param1",
        @"{name}.param_2":@"param2"
    }];
    

    说明:

    https://github.com/RestKit/RestKit/wiki/Object-mapping#handling-dynamic-nesting-attributes

    在上述 RestKit 文档中,当您尝试将键映射到属性时,系统会指示您使用 addAttributeMappingFromKeyOfRepresentationToAttribute,然后使用括号映射嵌套键以表示您的属性,后跟“.”。和你的嵌套键。

    在 RestKit 中,在从 addAttributeMappingFromKeyOfRepresentationToAttribute 属性映射执行映射后,它会循环遍历所有定义的属性映射,以将占位符替换为键中的实际值,以便可以进行额外的映射操作。在此过程中,它创建一个新的RKPropertyMapping 对象并设置它的sourceKeyPath 和destinationKeyPath。这些属性的属性设置器获取值并用大括号替换括号,然后 RestKit 不会找到带大括号的值的不正确映射。

    示例上下文:

    // JSON   
    { "blake": {
        "email": "blake@restkit.org",
        "favorite_animal": "Monkey"
        }
    }
    
    // Class
    @interface User : NSObject
    @property (nonatomic, copy) NSString* email
    @property (nonatomic, copy) NSString* username;
    @property (nonatomic, copy) NSString* favoriteAnimal;
    @end
    
    // Mapping
    RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[User class] ];
    [mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"username"];
    [mapping addAttributeMappingsFromDictionary:@{
        @"(username).email": @"email",
        @"(username).favorite_animal": @"favoriteAnimal"
    }];
    

    示例映射:

    // Resulting Attribute Mappings
    @"{username}.email": @"email",
    @"{username}.favorite_animal": @"favoriteAnimal"
    
    
    // 1. The attribute mapping has been performed for 'blake' > 'username'
    // 2. RestKit now loops through your attribute mappings to replace '{username}' with 'blake' so your further nested attribute mappings can take place
    // 3. Example: @"{username}.email": @"email"
          sourceKeyPath = @"{username}.email"
          destinationKeyPath = @"email"
    
          * replaces values *
          sourceKeyPath = @"blake.email"
          destinationKeyPath = @"email"
    
          * creates a new RKPropertyMapping object *
          RKPropertyMapping
            - sourceKeyPath = @"blake.email"
            - destinationKeyPath = @"email"
    

    带括号的示例映射:

    // JSON   
    { "blake (a.k.a GOAT)": {
        "email": "blake@restkit.org",
        "favorite_animal": "Monkey"
        }
    }
    
    // Resulting Attribute Mappings
    @"{username}.email": @"email",
    @"{username}.favorite_animal": @"favoriteAnimal"
    
    
    // 1. The attribute mapping has been performed for 'blake (a.k.a GOAT)' > 'username'
    // 2. RestKit now loops through your attribute mappings to replace '{username}' with 'blake (a.k.a GOAT)' so your further nested attribute mappings can take place
    // 3. Example: @"{username}.email": @"email"
          sourceKeyPath = @"{username}.email"
          destinationKeyPath = @"email"
    
          * replaces values *
          sourceKeyPath = @"blake (a.k.a GOAT).email"
          destinationKeyPath = @"email"
    
          * creates a new RKPropertyMapping object *
          RKPropertyMapping
            - sourceKeyPath = @"blake {a.k.a GOAT}.email" // DOES NOT MATCH
            - destinationKeyPath = @"email"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-31
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 2011-12-31
      相关资源
      最近更新 更多