저는 RestKit을 사용하여 내 iOS 프로젝트의 JSON 개체를 매핑합니다. 지금까지 모든 것이 잘 작동하지만 사용자 정의 유형의 하위 배열을 매핑하는 데 문제가 있습니다.RestKit - 다중 유형 하위 배열 매핑
는 JSON과 같은 같습니다 article_detail
{ "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",
}
},
]}
}]
}
참고 유형은 임의의 숫자 또는 주문에 표시 할 수있는 객체. 또한 이러한 개체가 나타나는 순서와 각각의 형식을 저장해야한다는 점에 유의해야합니다.
데이터 조작을 단순화하기 위해 CoreData를 사용하여 두 개의 NSManagedObject를 자동 생성했습니다. 뭔가 다음과 같이
//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)
이 기사의 정보를 잘 작동 :
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?
}
내 매핑은 다음과 같습니다. 그러나 예상대로,
fromKeyPath: "article_details.text"
때문에 "텍스트"개체 만 매핑됩니다.
유형을 쉽게 식별 할 수 있으며, 기사 상세 개체에서 어떤 매개 변수가 nil인지 확인합니다.
비슷한 문제
는 RestKit를 사용하여, 나는 스위프트에서이 작업을 수행 할 수있는 방법 RestKit - Map keypath of an array to the object inside of that array에서 찾을 수 있습니까?
대단히 감사합니다.
-N