我使用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"
它只映射"文字"对象。
识别类型很简单,检查文章详细信息对象中哪些参数为零。
可以找到类似的问题 RestKit - Map keypath of an array to the object inside of that array
如何使用RestKit在Swift中完成此操作?
非常感谢。
-N
答案 0 :(得分:0)
昨天我设法解决了这个问题。 我使用了一个类作为"包装器"对于每个自定义对象。
因此,本文只包含一组有序的Wrappers,如果这些对象保存顺序,并且包装器包含文章细节。像这样:
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