2011-07-17 12 views
7

特定の配列インデックスをRestKit(OM2)を持つプロパティにマップしたいと思います。私は、このJSONを持っている:RestKit mapKeyPath to配列インデックス

私は、このオブジェクトにマッピングしたい
{ 
    "id": "foo", 
    "position": [52.63, 11.37] 
} 

@interface NOSearchResult : NSObject 
@property(retain) NSString* place_id; 
@property(retain) NSNumber* latitude; 
@property(retain) NSNumber* longitude; 
@end 

私がのプロパティに私のJSONでの位置配列から値をマッピングする方法を見つけ出すことはできません私の客観的なクラスです。

RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[NOSearchResult class]]; 
[resultMapping mapKeyPath:@"id" toAttribute:@"place_id"]; 

ここで、緯度/経度のマッピングを追加するにはどうすればよいですか?私はいろいろ試してみましたが、うまくいきません。例えば:

[resultMapping mapKeyPath:@"position[0]" toAttribute:@"latitude"]; 
[resultMapping mapKeyPath:@"position.1" toAttribute:@"longitude"]; 

私のオブジェクトでlatitudeにJSONのうちposition[0]をマッピングする方法はありますか?

答えて

3

短い答えはいいえ - key-value codingは許されません。コレクションの場合は、max、min、avg、sumなどの集計操作のみがサポートされます。

あなたの最善の策は、NOSearchResultにNSArrayのプロパティを追加することが考えられます:

// NOSearchResult definition 
@interface NOSearchResult : NSObject 
@property(retain) NSString* place_id; 
@property(retain) NSString* latitude; 
@property(retain) NSNumber* longitude; 
@property(retain) NSArray* coordinates; 
@end 

@implementation NOSearchResult 
@synthesize place_id, latitude, longitude, coordinates; 
@end 

と、このようにマッピングを定義します。その後

RKObjectMapping* resultMapping = [RKObjectMapping mappingForClass:[NOSearchResult class]]; 
[resultMapping mapKeyPath:@"id" toAttribute:@"place_id"]; 
[resultMapping mapKeyPath:@"position" toAttribute:@"coordinates"]; 

、手動で座標から緯度と経度を割り当てることができます。

編集: - 私はすでにそれが動作しない恐れていた緯度/経度の割り当てを行うには良い場所は、オブジェクトローダ委譲し

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object; 

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects; 
+1

でおそらく感謝です。 'didLoadObject'ヒントは本当に役に立ちました! – cellcortex

+2

latとlonのカスタムgetterとsetterの方が、基本となる配列のデータ構造を操作する上でより良い場所になります。 – Jon

関連する問題