【发布时间】:2014-02-14 20:15:15
【问题描述】:
我有一个来宾实体,内部来宾是一个城市实体。我已经使用 RKRelationshipMapping 在 CoreData 和 RestKit 中设置了关系。
我已经正确映射了一个实体,但是当我使用 RKRelationshipMapping 添加一个城市实体时,在使用 @"cityId" 属性调用以下方法时似乎会生成此错误:
-[RKPropertyInspector classForPropertyNamed:ofEntity:]: unrecognized selector sent to instance
我的映射代码:
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:self.serverBaseAddress]];
objectManager.managedObjectStore = [RKManagedObjectStore defaultStore];
[RKObjectManager setSharedManager:objectManager];
RKEntityMapping *cityEMapping = [RKEntityMapping mappingForEntityForName:self.cityEntityName inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[cityEMapping addAttributeMappingsFromDictionary:@{
@"id" : @"cityId",
@"name" : @"name"
}];
RKEntityMapping *guestEMapping = [RKEntityMapping mappingForEntityForName:self.entityName inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[guestEMapping addAttributeMappingsFromDictionary:@{
@"name" : @"name",
@"photo" : @"photo",
@"comment" : @"comment",
@"id" : @"guestId",
@"date" : @"date"
}];
[guestEMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"city"
toKeyPath:@"city"
withMapping:cityEMapping]];
guestEMapping.identificationAttributes = @[ @"guestId" ];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:guestEMapping method:RKRequestMethodAny pathPattern:self.postGetAddress keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
以及应用停止的位置:(propertyClass 在 propertyClass = [[RKPropertyInspector sharedInspector]...中接收 0x00)
- (Class)classForKeyPath:(NSString *)keyPath
{
NSArray *components = [keyPath componentsSeparatedByString:@"."];
Class propertyClass = self.objectClass;
for (NSString *property in components) {
propertyClass = [[RKPropertyInspector sharedInspector] classForPropertyNamed:property ofClass:propertyClass isPrimitive:nil];
if (! propertyClass) propertyClass = [[RKPropertyInspector sharedInspector] classForPropertyNamed:property ofEntity:self.entity];
if (! propertyClass) break;
}
return propertyClass;
}
这是json:
[{"id":"3","name":"Convidado3","city":{"id":"2","name":"São Paulo"},"date":"Data2","comment":"comentario3","photo":":P"},
{"id":"2","name":"Convidado2","city":{"id":"1","name":"Rio de Janeiro"},"date":"Data2","comment":"comentario2","photo":":|"},
{"id":"1","name":"Convidado1","city":{"id":"1","name":"Rio de Janeiro"},"date":"Data1","comment":"comentario1","photo":":)"},
{"id":"5","name":"Convidado5","city":{"id":"4","name":"Vitória"},"date":"Data4","comment":"comentario5","photo":":K"},
{"id":"4","name":"Convidado4","city":{"id":"3","name":"Belo Horizonte"},"date":"Data3","comment":"comentario4","photo":";)"}]
实体Guest有一个名为city的关系,它通过一个名为guests的关系以下列方式连接到City实体:
客人.....城市
城市>客人
如果我只是删除它工作正常的关系,没有城市子对象... 任何帮助或建议表示赞赏...
【问题讨论】:
-
您不会错过城市地图中的标识属性吗?
-
您是否正确设置了链接器值?
-
好的,我已经在城市映射中添加了标识属性(在
RKEntityMapping *cityEMapping之后),cityEMapping.identificationAttributes = @[ @"cityId" ];现在错误在其他地方:static NSArray *RKArrayOfAttributesForEntityFromAttributesOrNames(NSEntityDescription *entity, NSArray *attributesOrNames)...我可以看到来宾实体和它有一个以 NSRelationshipDescription 作为值的城市属性。但是当代码运行时NSAttributeDescription *attribute = [[entity attributesByName] valueForKey:attributeOrName];属性仍然接收 nil... 这会引发NSException -
Wain,链接器值是什么意思?只是为了帮助澄清我是如何获得当前代码的,我一直在关注link,关系部分代码...
标签: objective-c core-data restkit