尝试获取实体属性的值时出错

时间:2015-08-04 00:21:38

标签: ios objective-c cocoa core-data

我正试图从实体属性中获取价值:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.mapaView setDelegate:self];


    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
    request.naturalLanguageQuery = self.loja.endereco; // <-- Here
    request.region = self.mapaView.region;

    MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];

    [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error)
    {
        NSMutableArray *placemarks = [NSMutableArray array];
        for (MKMapItem *item in response.mapItems) {
            [placemarks addObject:item.placemark];
        }
        [self.mapaView removeAnnotations:[self.mapaView annotations]];
        [self.mapaView showAnnotations:placemarks animated:NO];
    }];
}

但是我收到了这个错误:

2015-08-03 21:01:41.196 MinhaBrasilia[2465:69970] -[__NSCFDictionary endereco]: unrecognized selector sent to instance 0x7fb2faed1910
2015-08-03 21:01:41.199 MinhaBrasilia[2465:69970] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary endereco]: unrecognized selector sent to instance 0x7fb2faed1910'

Loja(self.loja)是一个实体,它是从plist创建的,使用NSPredicate进行过滤,由方法tableView:cellForRowAtIndexPath加载,并通过prepareForSegue:sender作为参数传递,全部在上一个屏幕中。< / p>

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     LojaTableViewCell *celula = [tableView dequeueReusableCellWithIdentifier:IdentificadorCelula
                                                                forIndexPath:indexPath];

     self.listaFiltrada = [self carregarPlistDeLojasComId:identificadorBtn filtradaPor:self.txtCategoria];

     self.loja = [NSEntityDescription insertNewObjectForEntityForName:@"Loja" inManagedObjectContext:self.managedObjectContext];

     [self.loja setValue:[[self.listaFiltrada objectAtIndex:indexPath.row] objectForKey:@"titulo"] forKey:@"titulo"];
     [self.loja setValue:[[self.listaFiltrada objectAtIndex:indexPath.row] objectForKey:@"subtitulo"] forKey:@"subtitulo"];
     [self.loja setValue:nil forKey:@"categoria"]; //Not implemented
     [self.loja setValue:[[self.listaFiltrada objectAtIndex:indexPath.row] objectForKey:@"endereco"] forKey:@"endereco"];
     [self.loja setValue:nil forKey:@"quadra"]; //Not implemented
     [self.loja setValue:[[self.listaFiltrada objectAtIndex:indexPath.row]  objectForKey:@"telefone"] forKey:@"telefone"];


     [celula preencherCelulaComTitulo:self.loja.titulo
                         comSubtitulo:self.loja.subtitulo
                         comCategoria:self.loja.categoria
                          comEndereco:self.loja.endereco
                            comQuadra:self.loja.quadra
                          comTelefone:self.loja.telefone];

    [celula setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
     return celula;
 }

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.loja = [self.listaFiltrada objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:SegueLoja sender:self.loja];
}

#pragma mark - Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:SegueLoja]) {
        DetalheViewController *detalheVC = (DetalheViewController *)[[segue destinationViewController] topViewController];
        [detalheVC setLoja:sender];
    }
}

显然我正在收到一本字典,如果我request.naturalLanguageQuery = [self.loja valueForKey:@"endereco"];,我可以获得我想要的属性,但这样我相信我没有使用CoreData的资源。

1 个答案:

答案 0 :(得分:1)

您收到的错误是因为self.loja是字典:didSelectRowAtIndexPath您有:

self.loja = [self.listaFiltrada objectAtIndex:indexPath.row];

这是从过滤后的plist派生的NSDictionary,而不是您在NSManagedObject中创建的cellForRowAtIndexPath。字典没有实现endereco方法,因此错误。它确实实现了valueForKey,因此可以工作:但是如果你遵循这条路线,你就是访问字典的键,而不是NSManagedObject。

我认为你应该重新考虑一下你的方法 - 使用cellForRowAtIndexPath将plist处理成NSManagedObject s是一个坏主意。它只会被调用为可见单元格,并且可能会为同一行调用多次(如果它在屏幕上滚动和关闭)。我会编写一个例程来处理所有plist到Core Data,然后生成一个NSManagedObject数组,然后你可以将它们用作表视图的数据源。