0

全部,

这一定是只有我缺乏的答案,因为我在任何地方都找不到答案。
我正在 ios 上学习 RestKit,并最终设法让它正确地使用 JSON,并带有关系(哇哦!)。

但是,我无法弄清楚如何从父类访问子类的字段/数据。

示例 JSON:{ events { name : "Fitness Walk", desc : "walking in circles" location { placename : "your backyard", longitude : -40.1234, latitude : 38.5678, address : "1600 Penn" } }

所以我有一个事件类和一个位置类。事件类有一个指向位置的指针作为成员变量(EventLocation * toLocation)来包含关系:

[RKObjectManager setSharedManager:objectManager];

RKEntityMapping * locationMapping = [RKEntityMapping mappingForEntityForName:@"EventLocation" inManagedObjectStore:managedObjectStore];
NSDictionary * locationInfo = @{ @"placename" : @"eventPlaceName", @"longitude" : @"longitude", @"latitude" : @"latitude", @"address" : @"eventAddress" };
[locationMapping addAttributeMappingsFromDictionary:locationInfo];

RKEntityMapping * eventMapping = [RKEntityMapping mappingForEntityForName:@"NYCEvent" inManagedObjectStore:managedObjectStore];
NSDictionary * eventInfo = @{ @"_id" : @"event_id", @"name" : @"event_name",@"desc" : @"event_description" };
[eventMapping addAttributeMappingsFromDictionary:eventInfo];

[eventMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"location" toKeyPath:@"toLocation" withMapping:locationMapping]];

RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:eventMapping pathPattern:@"/api/search" keyPath:@"events" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

和教程一样,我想将 eventPlaceName 文本作为副标题放在 UITableViewCell 中。

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[object valueForKey:@"event_name"] description];


    NSString * lbl = < somehow get the event place name>;


    cell.detailTextLabel.text = lbl;
}

但我不知道怎么做。请帮帮我?

谢谢!:bp:

4

1 回答 1

0

您可以使用键值编码:

NSManagedObject *object = ... // your Event object
NSString *placeName = [object valueForKeyPath:@"toLocation.eventPlaceName"];

如果您创建托管对象子类(Xcode->Editor->Create NSManagedObject Subclass ...),它会变得更容易。然后您可以使用对象的属性:

Event *event = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = event.event_name;
NSString *lbl = event.toLocation.eventPlaceName;
于 2013-06-27T21:11:04.030 回答