2016-10-28 10 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を使用して2つの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