提前致谢所有帮助社区!!我有一个早期的问题,这是我指出的,我修复了“[人物对象] - 应该是 - > [人员分配]初始化]我修复了它,现在能够将一个Person对象添加到我的服务器。但是这里问题是服务器上的所有值都为空并且它返回1001代码Code = 1001“无法找到keyPath的对象映射:''”UserInfo = 0x5938ce0 {= RKObjectMapperKeyPath,NSLocalizedDescription =无法找到keyPath的对象映射:''} 什么是我错误地映射任何想法? Noobie在这里,感谢您提供的任何帮助! 哦,如果你在社交中,我愿意为这个问题付课。 Thnx再次!
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[Person class]];
[userMapping mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];
[userMapping mapKeyPath:@"created_at" toAttribute:@"createdAt"];
[userMapping mapKeyPath:@"name" toAttribute:@"name"];
[userMapping mapKeyPath:@"id" toAttribute:@"personId"];
RKObjectMapping* dogMapping = [RKObjectMapping mappingForClass:[Dog class]];
[dogMapping mapKeyPath:@"created_at" toAttribute:@"createdAt"];
[dogMapping mapKeyPath:@"person_id" toAttribute:@"spersonId"];
[dogMapping mapKeyPath:@"name" toAttribute:@"name"];
[dogMapping mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];
[dogMapping mapKeyPath:@"id" toAttribute:@"dogId"];
RKObjectMapping *dataMapping = [RKObjectMapping mappingForClass:[Data class]];
[dataMapping mapKeyPath:@"dog" toAttribute:@"dogs"];
[dataMapping mapKeyPath:@"person" toRelationship:@"person" withMapping:userMapping];
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:dataMapping];
[[RKObjectManager sharedManager].mappingProvider setMapping:userMapping
forKeyPath:@"people"];
RKObjectRouter * router = [RKObjectManager sharedManager].router;
[router routeClass: [Person class] toResourcePath:@"/people/:personId"];
[router routeClass: [Person class] toResourcePath:@"/people"
forMethod:RKRequestMethodPOST];
RKObjectMapping *personSerializationMapping= [RKObjectMapping mappingForClass:
[NSMutableDictionary class]];
[personSerializationMapping mapAttributes:@"name", nil];
[[RKObjectManager sharedManager].mappingProvider
setSerializationMapping:personSerializationMapping forClass: [Person class]];
Person* daveLiu =[[[Person alloc]init]autorelease];
daveLiu.name = @"dave";
[[RKObjectManager sharedManager] postObject:daveLiu delegate:self];
答案 0 :(得分:1)
我的服务器中的所有对象都是“匿名的”,而RestKit也无法找到正确的映射。我修复它的方法是使用objectManager方法,让我包含映射:
[objectManager postObject:object mapResponseWith:[object objectMapping] delegate:self];
[objectManager loadObjectsAtResourcePath:path objectMapping:mapping delegate:self];
所以现在我的合同都来自服务器这样:
{
"Id": "12345678-1234-1234-1234-1234567890ab",
"Type": "SURVEY",
"Code": "1234",
"Description": "blah blah blah",
"Message": "blah blah blah",
"Url": "http://nowhere.com/",
"NextAttemptHours": 4
}
可以映射到正确的对象。保持对象映射可用是一项额外的工作,但它可以完成任务。我是通过在我的对象上实现一个类方法来返回适当的objectMapping来实现的,它使事情变得非常干净和易懂。