【问题标题】:How can I map different JSON inputs to the same object in RestKit?如何将不同的 JSON 输入映射到 RestKit 中的同一个对象?
【发布时间】:2013-02-21 18:58:56
【问题描述】:

我在建模正常的 JSON 对象时没有问题,但现在为了国际化,我想建模以下场景,其中属性可以是字符串或键/值对字典:

"objects": [{"title": "non internatinalizaed title", ...},
    {"title": {"en": "english string", "es": "spanish string"}, ...},
    ....]

由于我不知道服务器是要发送单个字符串还是多个字符串,所以我想创建一个这样的模型对象:

@interface I18NString : Object
@property (copy) NSString *forced; // Set when there is no i18n
@property (copy) NSDictionary *strings; // Set with i18n.
- (NSString*)stringValue; // Returns forced or one of strings

类似于 C 结构联合,取决于 strings 不是 nil 我会在 stringValue 方法中选择一个属性或另一个。我已经阅读了 restkit wiki 中的 Dynamic object mapping 部分,但我看不到如何使用它来解决这个问题。

【问题讨论】:

    标签: objective-c json model internationalization restkit


    【解决方案1】:

    您要建模的是一对多关系,其中很多可能是 500、1 或 0。所以,服务器发送一个只有一个键/值(甚至 0,尽管我猜你的许多关系是 1..n 而不是 0..n)

    【讨论】:

      猜你喜欢
      • 2012-10-08
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      相关资源
      最近更新 更多