Ios 使用RestKit序列化具有子对象的对象[发布]

Ios 使用RestKit序列化具有子对象的对象[发布],ios,restkit,Ios,Restkit,我设置了以下对象: RKObjectMapping* geoPointMapping = [RKObjectMapping mappingForClass:[CRGeoPoint class]]; geoPointMapping.setDefaultValueForMissingAttributes = YES; [geoPointMapping mapKeyPathsToAttributes: @"longitude", @"longitude", @"latitude",

我设置了以下对象:

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"];
但当我尝试发送查询时(使用几何体对象)。我保持 获取此错误:

收到错误错误域=JKErrorDomain代码=-1“无法序列化 对象类CRGeoPoint。“


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

查看有关对象序列化的部分。然后看一看,让自己确信这些事情是分开的。

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]];

谢谢你给我的文档指针,你和Shane都部分正确。但是医生指出了完整答案的方向。我需要做的不仅是你必须创建反向映射,而且你还必须指定要用于关系的映射。是的,我对此困惑了好一阵子。最终我意识到我需要向映射提供者注册映射(由资源路径设置密钥),并向router'单独注册类资源路径。