RestKit XML 嵌套元素的映射问题

RestKit XML Mapping issue with nested elements

我正在使用 RestKit .22,并且在映射 XML 对实体的响应时遇到问题。一切正常,我得到 200OK,但对象映射不正确。在 XML 响应中有一个根元素 "response",在它下面是一个 "events".

的数组
<response>
  <meta>...</meta>
  <events>
      <event>...// data that I'm after reflected in the entity. </event>
  </events>
</response>

我的代码:

- (void)setupRestKitWithBaseURL:(NSURL *)baseURL{
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:RKMIMETypeXML];
[objectManager setAcceptHeaderWithMIMEType:@"application/xml"];

// Initialize managed object store
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;

NSEntityDescription *entity = [[managedObjectStore.managedObjectModel entitiesByName] objectForKey:@"Event"];
RKEntityMapping *eventMapping = [[RKEntityMapping alloc] initWithEntity:entity];
[eventMapping addAttributeMappingsFromArray:@[ @"title" ]];
[eventMapping addAttributeMappingsFromDictionary:@{
                                                   @"id" : @"remoteID",
                                                   @"date_tbd" : @"dateToBeDecided",
                                                   @"time_tbd" : @"timeToBeDecided",
                                                   @"datetime_utc" : @"start",
                                                   @"venue.name" : @"location"
                                                   }];

RKResponseDescriptor *eventIndexResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:eventMapping method:RKRequestMethodGET pathPattern:@"events" keyPath:@"response" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:eventIndexResponseDescriptor];


[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"events"];
    NSDictionary *argsDict = nil;
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
    if (match) {
        return [NSFetchRequest fetchRequestWithEntityName:@"Event"];
    }
    return nil;
}];

[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Example.sqlite"];
[managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:nil];
[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

}

控制台:

[63994:2621733] I restkit.network:RKObjectRequestOperation.m:220 GET 'http://api.seatgeek.com/2/events?format=xml&lat=39.75&lon=-104.984&per_page=500&range=10mi&taxonomies.name=sports' (200 OK / 2 objects) [request=0.0002s mapping=0.0018s total=0.0035s]

所以我加载了 2 个事件,它们是 "response" 元素的嵌套元素:"meta" & "events",但不是 "events" 数组下的实际事件,这就是我需要。非常感谢帮助。 谢谢

更新

除了 Wain 的回答外,我还将这些值更改为 value.text:

[eventMapping addAttributeMappingsFromDictionary:@{
                                                   @"title.text" : @"title",
                                                   @"id.text" : @"remoteID",
                                                   @"date_tbd.text" : @"dateToBeDecided",
                                                   @"time_tbd.text" : @"timeToBeDecided",
                                                   @"datetime_utc.text" : @"start",
                                                   @"venue.name.text" : @"location"
                                                   }];

您需要指定完整的键路径才能向下钻取到映射级别。根据您的描述,它应该是 response.events.event 但您刚刚设置了 response.