2016-10-28 3 views
0

저는 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

답변

0

나는 어제이 문제를 해결할 수있었습니다. 클래스를 각 사용자 지정 개체의 "래퍼"로 사용했습니다.

그래서 기사에는 단순히 래퍼의 정렬 된 집합이 포함되어 있으며 이러한 개체가있는 경우 순서를 저장하고 래퍼에 기사 세부 정보가 포함되어 있습니다. 그래서 같이 :

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