使用RestKit使用子对象序列化对象[POSTing]

时间:2012-03-13 21:16:58

标签: ios restkit

我有以下对象设置:

RKObjectMapping* geoPointMapping = [RKObjectMapping mappingForClass:[CRGeoPoint class]];

geoPointMapping.setDefaultValueForMissingAttributes = YES;
[geoPointMapping mapKeyPathsToAttributes:
    @"longitude", @"longitude",
    @"latitude", @"latitude",
    nil];
[objectManager.mappingProvider registerMapping:geoPointMapping withRootKeyPath:@"geometry"];

RKObjectMapping* criteriaMapping = [RKObjectMapping mappingForClass:[CRCriteria class]];

criteriaMapping.setDefaultValueForMissingAttributes = YES;
[criteriaMapping mapKeyPathsToAttributes:
    @"type", @"type",
    @"geometry", @"geometry",
    @"fromDate", @"fromDate",
    @"toDate", @"toDate",
    @"radius", @"radius",
    nil];
[objectManager.mappingProvider registerMapping:criteriaMapping withRootKeyPath:@"criteria"];

但是当我尝试发送查询(带有几何对象)时。我一直 收到此错误:

  收到

错误错误Domain = JKErrorDomain Code = -1“无法序列化   对象类CRGeoPoint。“

2 个答案:

答案 0 :(得分:2)

入站和出站映射信息由RKObjectMappingProvider单独处理。您已在该代码中配置了入站映射(从服务器下载数据)。但是 - RestKit组上的as pointed out by Shane Zatezalo - 您还需要为RestKit添加序列化映射,以了解如何将对象转换为JSON(或者您可能正在使用的其他任何序列化格式)。

查看有关对象序列化的Object Mapping tutorial's部分。并查看source for the mapping provider以确保自己这些事情是分开的。

答案 1 :(得分:0)

OP解决方案。

解决方案是双重的。首先我必须指定逆向映射,如下面的Sixten所指出的,其次我必须指定用于CRCriteria对象上的CRGeoPoint类的映射。代码如下:

RKObjectMapping* geoPointMapping = [RKObjectMapping mappingForClass:[CRGeoPoint class]];

geoPointMapping.setDefaultValueForMissingAttributes = YES;

[objectManager.mappingProvider registerMapping:geoPointMapping withRootKeyPath:@"geometry"];

// Build a serialization mapping by inverting our object mapping. Includes attributes and relationships
RKObjectMapping* geoPointSerializationMapping = [geoPointMapping inverseMapping];
// You can customize the mapping here as necessary -- adding/removing mappings
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:geoPointSerializationMapping forClass:[CRGeoPoint class]]; 

RKObjectMapping* criteriaMapping = [RKObjectMapping mappingForClass:[CRCriteria class]];

criteriaMapping.setDefaultValueForMissingAttributes = YES;
[criteriaMapping mapKeyPathsToAttributes:
 @"type", @"type",
 @"fromDate", @"fromDate",
 @"toDate", @"toDate",
 @"radius", @"radius",
 nil];
[criteriaMapping mapKeyPath:@"geometry" toRelationship:@"geometry" withMapping:geoPointMapping];
[objectManager.mappingProvider registerMapping:criteriaMapping withRootKeyPath:@"criteria"];

// Build a serialization mapping by inverting our object mapping. Includes attributes and relationships
RKObjectMapping* criteriaSerializationMapping = [criteriaMapping inverseMapping];
// You can customize the mapping here as necessary -- adding/removing mappings
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:criteriaSerializationMapping forClass:[CRCriteria class]];