RestKit - 多类型子数组映射
RestKit - Multi-type subarray mapping
我正在使用 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 自动生成了两个 NSManagedObject。内容如下:
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"
它只映射 "text" 个对象。
识别类型很容易,检查文章详细信息对象中哪些参数为 nil。
类似的问题可以在
RestKit - Map keypath of an array to the object inside of that array
如何使用 RestKit 在 Swift 中完成此操作?
非常感谢。
-N
我昨天设法解决了这个问题。
我为每个自定义对象使用 class 作为 "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
我正在使用 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 自动生成了两个 NSManagedObject。内容如下:
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"
它只映射 "text" 个对象。
识别类型很容易,检查文章详细信息对象中哪些参数为 nil。
类似的问题可以在 RestKit - Map keypath of an array to the object inside of that array
如何使用 RestKit 在 Swift 中完成此操作?
非常感谢。
-N
我昨天设法解决了这个问题。 我为每个自定义对象使用 class 作为 "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