2012-04-24 15 views
6

ネストされた配列をマッピングするための質問があります。 https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.mdには、ネストされたオブジェクトをマッピングする例があります。Restart Mapping Nested Array

{ "articles": [ 
    { "title": "RestKit Object Mapping Intro", 
     "body": "This article details how to use RestKit object mapping...", 
     "author": [{ 
      "name": "Blake Watters", 
      "email": "[email protected]" 
     }, 
     { 
      "name": "abc", 
      "email": "emailaddress" 
     }] 
     "publication_date": "7/4/2011" 
    }] 
} 

がどのように私のクラスは、著者の配列を取得するために、次のようになります。ネストされたオブジェクトは、JSONのように見える例えばオブジェクトの配列が、ある場合

しかし、私は、何をすべきかを持っているのですか?例のコードザッツ :

@interface Author : NSObject 
    @property (nonatomic, retain) NSString* name; 
    @property (nonatomic, retain) NSString* email; 
@end 



@interface Article : NSObject 
    @property (nonatomic, retain) NSString* title; 
    @property (nonatomic, retain) NSString* body; 
    @property (nonatomic, retain) Author* author; // should be an array of Author-Objects 
    @property (nonatomic, retain) NSDate* publicationDate; 
@end 

私は著者の配列をtheresのと私はNSArrayのに条にauthor属性のクラスを変更していた場合、どうするかRestkitがでている、知っているRestkitを、伝えることができますどのようにこのNSArrayはAuthor-Objects ...でなければなりません

私はRKObjectMappingを使用してObjectをJSONからObjective-cに、またはその逆にマッピングしています。

答えて

0

全体respnse文字列がnsmutable辞書としてそれを取る、あなたは配列をnamutableするために、著者値を割り当てる。..

7

あなたはそれに応じVARタイプを設定してくださいしたいと思う:

@interface Article : NSObject 
    @property (nonatomic, retain) NSString* title; 
    @property (nonatomic, retain) NSString* body; 
    @property (nonatomic, retain) NSSet* authors; 
    @property (nonatomic, retain) NSDate* publicationDate; 
@end 

それは可能NSArrayとして宣言して動作しますが、私は個人的にCoreDataモデルでRestKitを使用しています。これらのシナリオでの関係はNSSetです。

また、あなたは、マッピングを設定する必要があります。

[articleMapping mapKeyPath:@"author" toRelationship:@"authors" withMapping:authorMapping]; 
3

それを分割ができます。