gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:16:47 25 4
gpt4 key购买 nike

我正在尝试从实体属性中获取值:

- (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,而不是您在 cellForRowAtIndexPath 中创建的 NSManagedObject。该字典未实现 endereco 方法,因此出现错误。它确实实现了 valueForKey,因此它会起作用:但是如果您遵循这条路线,您将访问字典的键,而不是 NSManagedObject。

我认为您应该重新考虑您的方法 - 使用 cellForRowAtIndexPath 将 plist 处理成 NSManagedObject 是一个坏主意。它只会为可见单元格调用,并且可能会为同一行调用多次(如果它在屏幕上和屏幕外滚动)。我会编写一个例程来将所有 plist 处理成核心数据,并生成一个 NSManagedObject 数组,然后您可以将其用作 TableView 的数据源。

关于ios - 尝试获取实体属性的值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31798799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com