1

我正在使用 RestKit 将数据映射到两个类(区域和类别)中的属性,以便在两个表视图(区域和类别)中使用。但是,它似乎只是为我在测试应用程序时进入的第一个视图正确映射数据。例如,当我首先查看类别,然后切换区域时,我的区域将存储为类别类型的对象。如果我先查看区域,然后查看类别,我的类别将被映射为区域类型的对象。由于对象的数量,我可以说正在检索正确的信息,但它没有被存储为正确的类型。如何确保每个映射请求都正确完成?每个映射的代码如下:

分类:

RKURL *baseURL = [RKURL URLWithBaseURLString:@"MyUrl"];
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];
objectManager.client.baseURL = baseURL;

RKObjectMapping *categoryMapping = [RKObjectMapping mappingForClass:[Category class]];
[categoryMapping mapKeyPathsToAttributes:@"categoryID", @"categoryID", @"parentID", @"parentID", @"categoryName", @"categoryName", @"childrenCount", @"childrenCount", @"parentCount", @"parentCount", @"catCount", @"catCount", nil];
[objectManager.mappingProvider setMapping:categoryMapping forKeyPath:@""];

地区:

RKURL *baseURL = [RKURL URLWithBaseURLString:@"MyUrl"];
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];
objectManager.client.baseURL = baseURL;

NSLog(@"URL Created");

RKObjectMapping *regionMapping = [RKObjectMapping mappingForClass:[Region class]];
[regionMapping mapKeyPathsToAttributes:@"regionHome", @"regionHome", @"regionID", @"regionID", @"regionName", @"regionName", @"parentCount", @"parentCount", @"parentID", @"parentID", @"childrenCount", @"childrenCount", @"parentName", @"parentName", nil];
[objectManager.mappingProvider setMapping:regionMapping forKeyPath:@""];
4

1 回答 1

0

根据我的判断,您不想像@"". [objectManager.mappingProvider setMapping:regionMapping forKeyPath:@""];您可能会将这些替换为@"region"@"category"因此 RestKit 可以根据关键路径来判断它应该期望什么对象。

还有,你是用isKindOfClass:objectLoaderdidLoadObjects方法中的方法来区分对象的吗?

也许这会帮助你:

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {

  if ([[objects objectAtIndex:0] isKindOfClass:[Region class]]) {

      Region *region = [objects objectAtIndex:0];

  }
  else if ([[objects objectAtIndex:0] isKindOfClass:[Category class]]) {

      Category *category = [objects objectAtIndex:0];

  }
}
于 2012-09-19T04:22:28.950 回答