RestKit RKObjectMapping 到 CLLocation

发布于 2024-12-13 21:51:08 字数 858 浏览 1 评论 0原文

我正在使用 RestKit 的对象映射将 JSON 数据映射到对象。 是否可以将纬度和经度 JSON 属性映射到 Objective-C 类中的 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"];

非常感谢您的帮助。

I am using RestKit's Object Mapping to map JSON data to an object.
Is it possible to map the latitude and longitude JSON attributes to a CLLocation variable in my Objective-C class?

The JSON:

{ "items": [
    {
        "id": 1,
        "latitude": "48.197186",
        "longitude": "16.267452"
    },
    {
        "id": 2,
        "latitude": "48.199615",
        "longitude": "16.309645"
    }
]

}

The class it should be mapped to:

@interface ItemClass : NSObject    
  @property (nonatomic, strong) CLLocation *location;
@end

In the end I'd like to call itemClassObj.location.longitude getting me the value of the latitude from the JSON response.

I thought something like this would work, but it doesn't.

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[ItemClass class]];
[mapping mapKeyPath:@"latitude" toAttribute:@"location.latitude"];
[mapping mapKeyPath:@"longitude" toAttribute:@"location.longitude"];

Thanks a lot for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缘字诀 2024-12-20 21:51:08

RestKit 专门为 CLLocation 添加了一个 ValueTransformer:

https://github.com/RestKit/RKCLLocationValueTransformer

给出示例JSON:

{
    "user": {
        "name": "Blake Watters",
        "location": {
            "latitude": "40.708",
            "longitude": "74.012"
        }
    }
}

要从给定的 JSON 映射到用户对象:

@interface User : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) CLLocation *location;
@end

使用 RKCLLocationValueTransformer:

#import "RKCLLocationValueTransformer.h"

RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping addAttributeMappingsFromArray:@[ @"name" ]];
RKAttributeMapping *attributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"location" toKeyPath:@"location"];
attributeMapping.valueTransformer = [RKCLLocationValueTransformer locationValueTransformerWithLatitudeKey:@"latitude" longitudeKey:@"longitude"];
[userMapping addPropertyMapping:attributeMapping];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"user" statusCodes:[NSIndexSet indexSetWithIndex:200]];

RestKit has added a ValueTransformer specifically for CLLocation:

https://github.com/RestKit/RKCLLocationValueTransformer

Given the example JSON:

{
    "user": {
        "name": "Blake Watters",
        "location": {
            "latitude": "40.708",
            "longitude": "74.012"
        }
    }
}

To map from the given JSON to a User object:

@interface User : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) CLLocation *location;
@end

Use the RKCLLocationValueTransformer:

#import "RKCLLocationValueTransformer.h"

RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
[userMapping addAttributeMappingsFromArray:@[ @"name" ]];
RKAttributeMapping *attributeMapping = [RKAttributeMapping attributeMappingFromKeyPath:@"location" toKeyPath:@"location"];
attributeMapping.valueTransformer = [RKCLLocationValueTransformer locationValueTransformerWithLatitudeKey:@"latitude" longitudeKey:@"longitude"];
[userMapping addPropertyMapping:attributeMapping];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"user" statusCodes:[NSIndexSet indexSetWithIndex:200]];
廻憶裏菂餘溫 2024-12-20 21:51:08

要创建 CLLocation,您同时需要纬度和经度。此外,CLLocation(如 CLLocationCooperative2D)的坐标不是 NSNumber,它们是双浮点数,因此它可以提高像这样的映射中的键值合规性,因为浮点数不是对象。

大多数情况下,人们会将纬度和经度存储在类中作为 NSNumbers,然后在实例化/填充类对象后根据需要构建 CLLocationCooperative2D 坐标。

如果您愿意的话,您可以做的是利用 willMapData: delegate 方法来监听传入的数据,以便手动填充 CLLocation ...但对我来说,这有点过分了,需要太多的开销。


编辑:添加此内容是因为注释不会格式化代码属性...

或者,您可以在对象类实现和接口中放置类似的内容...

@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;

- (CLLocationCoordinate2D)coordinate {
    CLLocationDegrees lat = [self.latitude doubleValue];
    CLLocationDegrees lon = [self.longitude doubleValue];
    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(lat, lon);
    if (NO == CLLocationCoordinate2DIsValid(coord))
        NSLog(@"Invalid Centroid: lat=%lf lon=%lf", lat, lon);
    return coord;
}

To create a CLLocation you need both the latitude and the longitude at the same time. Moreover, the coordinates for CLLocation (like CLLocationCoordinate2D) aren't NSNumbers, they're double floats, so it can jack up the key value compliance in mapping like this because floats aren't objects.

Most often, people will store a latitude and longitude in a class as NSNumbers and then build the CLLocationCoordinate2D coordinate on demand after the class object is instantiated/populated.

What you could do, if you were so inclined, is utilize the willMapData: delegate method to snoop the data coming in, in order to manually populate a CLLocation ... but to me this is overkill and requires too much overhead.


EDIT: Adding this because comments don't format code property...

Or, you could put something like this in your object class implementation and interface...

@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;

- (CLLocationCoordinate2D)coordinate {
    CLLocationDegrees lat = [self.latitude doubleValue];
    CLLocationDegrees lon = [self.longitude doubleValue];
    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(lat, lon);
    if (NO == CLLocationCoordinate2DIsValid(coord))
        NSLog(@"Invalid Centroid: lat=%lf lon=%lf", lat, lon);
    return coord;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文