我正在使用RestKit的對象映射將JSON數據映射到對象。 是否可以將我的Objective-C類中的緯度和經度JSON屬性映射到CLLocation
變量?RestKit RKObjectMapping到CLLocation
的JSON:
{ "items": [
{
"id": 1,
"latitude": "48.197186",
"longitude": "16.267452"
},
{
"id": 2,
"latitude": "48.199615",
"longitude": "16.309645"
}
]
}
類應該映射到:
@interface ItemClass : NSObject
@property (nonatomic, strong) CLLocation *location;
@end
最後,我想打電話itemClassObj.location.longitude
讓我的價值來自JSON響應的緯度。
我以爲這樣的事情會起作用,但事實並非如此。
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ItemClass class]];
[mapping mapKeyPath:@"latitude" toAttribute:@"location.latitude"];
[mapping mapKeyPath:@"longitude" toAttribute:@"location.longitude"];
非常感謝您的幫助。
是否有一種方法,火災在我的模型中,只要一切都被映射了?然後,我可以簡單地將映射的經緯度值分配給CLLocationCoordinate ... – alex
爲什麼不在您的模型上創建一個只讀屬性來爲您做這個?只要你準備好將它放在地圖上,就可以調用它......由於評論無法格式化代碼,所以請參閱上面的編輯。 –
此外,我認爲就對你的後映射問題的直接反應而言,'objectMapperDidFinishMapping:'? –