【问题标题】:RestKit - Multi-type subarray mappingRestKit - 多类型子数组映射
【发布时间】:2017-03-11 11:21:11
【问题描述】:

我正在使用 RestKit 在我的 iOS 项目中映射 JSON 对象。 到目前为止一切正常,但我在映射自定义类型的子数组时遇到了问题。

JSON 如下所示:

{ "Result" : "OK", "Total": "1","content": [ 
    {"article": 
        {
        "title": "sample_title",
        "author": "sample_author",
        "article_details": [
            {"text": 
                {
                "body": "sample_body",
                }
            },
            {"image": 
                {
                "image": "sample_image"
                }
            },
            {"quote": 
                {
                "quote": "sample_quote",
                }
            }
            {"text": 
                {
                "body": "sample_body",
                }
            },
        ]}
    }]
}

请注意,article_detail 对象类型可以以任意数量或顺序出现。 此外,重要的是要注意我需要存储这些对象出现的顺序,以及它的每个类型。

为此,为了简化数据操作,我使用 CoreData 自动生成了两个 NSManagedObjects。如下:

extension Article {
    public var title: String?
    public var author: String?
    public var details: NSOrderedSet?
}

extension ArticleDetail {
    public var body: String?
    public var image: String?
    public var quote: String?
}

我的映射如下:

//Detail mapping
let detailsMapping = RKEntityMapping(forEntityForName: "ArticleDetail", in: objectManager.managedObjectStore)
detailsMapping?.addAttributeMappings(from: [
    "image"     : "image",
    "body"      : "body",
    "quote"     : "quote",
    ]
)

//Article mapping
let articleMapping = RKEntityMapping(forEntityForName: "Article", in: objectManager.managedObjectStore)
articleMapping?.addAttributeMappings(from: [
    "title"          : "title",
    "author"         : "author",
    ]
)

//Relation mapping
articleMapping!.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "article_details.text", toKeyPath: "details", with: detailsMapping))

//Response descriptior
let articleResponseDescriptor = RKResponseDescriptor(
    mapping: articleMapping,
    method: RKRequestMethod.GET,
    pathPattern: nil,
    keyPath: "content.article",
    statusCodes: IndexSet(integer: 200)
)
objectManager.addResponseDescriptor(articleResponseDescriptor)

这适用于文章信息。然而,正如预期的那样,由于

fromKeyPath: "article_details.text"

它只映射“文本”对象。

识别类型很容易,检查文章详细对象中哪些参数为nil。

类似的问题可以在 RestKit - Map keypath of an array to the object inside of that array

我怎样才能在 Swift 中使用 RestKit 来完成这项工作?

非常感谢。

-N

【问题讨论】:

    标签: ios json swift core-data restkit


    【解决方案1】:

    我昨天设法解决了这个问题。 我使用一个类作为每个自定义对象的“包装器”。

    因此,文章仅包含一组有序的 Wrappers,如果这些对象保存顺序,则该 wrapper 包含文章详细信息。像这样:

    extension Article {
        public var title: String?
        public var author: String?
        public var details: NSOrderedSet?
    }
    
    extension ArticleWrapper {
        public var app_text: ArticleTextDetail?
        public var app_image: ArticleImageDetail?
        public var app_quote: ArticleQuoteDetail?
    }
    
    extension ArticleTextDetail {
        ...
    }    
    
    extension ArticleImageDetail {
        ...
    }  
    
    extension ArticleQuoteDetail {
        ...
    } 
    

    映射如下所示:

    let wrapperMapping = RKEntityMapping(forEntityForName: "ArticleWrapper", in: objectManager.managedObjectStore)
    
    
    let textMapping = RKEntityMapping(forEntityForName: "ArticleTextDetail", in: objectManager.managedObjectStore)
    ...
    
    let imageMapping = RKEntityMapping(forEntityForName: "ArticleImageDetail", in: objectManager.managedObjectStore)
    ...
    
    let quoteMapping = RKEntityMapping(forEntityForName: "ArticleQuoteDetail", in: objectManager.managedObjectStore)
    ...
    
    let articleMapping = RKEntityMapping(forEntityForName: "Article", in: objectManager.managedObjectStore)
    articleMapping?.addAttributeMappings(from: [
    "title"          : "title",
    "author"         : "author",
        ]
    )
    
    wrapperMapping!.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "text", toKeyPath: "app_text", with: textMapping))
    wrapperMapping!.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "image", toKeyPath: "app_image", with: imageMapping))
    wrapperMapping!.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "quote", toKeyPath: "app_quote", with: quoteMapping))
    
    articleMapping!.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "article_details", toKeyPath: "details", with: wrapperMapping))
    

    现在一切正常。

    谢谢。

    -N

    【讨论】:

      猜你喜欢
      • 2013-05-26
      • 2014-01-20
      • 2017-12-15
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      相关资源
      最近更新 更多