0
这一定是一个答案,只有我缺乏,因为我无法在任何地方找到答案。
我正在学习ios上的RestKit,并最终设法让它正确使用JSON,并完成关系(woohoo!)。
但是,我无法弄清楚我是如何从父类访问子类的字段/数据。
例JSON: { 事件{ 名称: “健身走”, 说明: “走在圈子里” 位置{ 地名: “你家后院”, 经度:-40.1234, 纬度: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:
谢谢!虽然它没有像广告中那样工作。我最终做的是:'Location * location = [event valueForKey:@“toLocation]; NSString * lbl = location.eventPlaceName;' –
@BillyPilgrim:'[event valueForKey:@”toLocation]''和'event.toLocation' *应该*相当。你提出的代码是否有编译器错误? –
没有编译器错误 - 运行时。进入杂草,发生核心数据错误。 “发生严重错误”或类似的情况 –