我正在尝试从实体属性获取价值:
- (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
作为参数传递的实体,所有这些均在上一屏幕中显示。 - (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的资源。 最佳答案
您收到的错误是因为self.loja
是一个字典:在didSelectRowAtIndexPath
中,您有:
self.loja = [self.listaFiltrada objectAtIndex:indexPath.row];
这是从过滤的plist派生的NSDictionary,而不是您在
NSManagedObject
中创建的cellForRowAtIndexPath
。该词典未实现endereco
方法,因此会出现错误。它确实实现了valueForKey
,因此可以正常工作:但是,如果您遵循此路线,则将访问字典的键,而不是NSManagedObject。我认为您应该重新考虑您的方法-使用
cellForRowAtIndexPath
将plist处理为NSManagedObject
是一个坏主意。仅在可见的单元格中会调用它,并且同一行(如果在屏幕上和屏幕外滚动)可能会多次调用。我将编写一个例程来将所有plist处理为Core Data,并生成一个NSManagedObject
数组,然后可以将其用作表视图的数据源。