7

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

{
  "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 の位置配列から値を object-c クラスのプロパティにマップする方法がわかりません。これまでのマッピングは次のようになります。

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

緯度/経度のマッピングを追加するにはどうすればよいですか? いろいろ試しましたがダメでした。例えば:

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

position[0]JSON からlatitudeオブジェクトにマップする方法はありますか?

4

1 に答える 1

3

簡単に言うと、いいえです。キー値のコーディングでは、それが許可されていません。コレクションでは、max、min、avg、sum などの集計操作のみがサポートされています。

あなたの最善の策は、おそらく NSArray プロパティを NOSearchResult に追加することです:

// 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;
于 2011-07-17T12:55:52.393 に答える