RestKit - 如何获取子对象数据?

时间:2013-06-27 20:58:22

标签: ios objective-c restkit

所有

这一定是我唯一缺乏的答案,因为我无法在任何地方找到答案 我正在ios上学习RestKit,并且终于设法让它正确地使用JSON,完成了一段关系(哇哦!)。

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

示例JSON:    {      事件{       名称:“健身步行”,       desc:“走在圈子里”       地点 {           地名:“你的后院”,           经度:-40.1234,           纬度:38.5678,           地址:“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;
}

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

谢谢! :沸点:

1 个答案:

答案 0 :(得分:0)

您可以使用键值编码:

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

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

Event *event = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = event.event_name;
NSString *lbl = event.toLocation.eventPlaceName;