Ios 尝试获取实体属性的值时出错
我正在尝试从实体属性获取值:Ios 尝试获取实体属性的值时出错,ios,objective-c,cocoa,core-data,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 r
- (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];
}];
}
Loja(self.Loja)是一个实体,它是从plist创建的,使用NSPredicate过滤,通过方法tableView:cellforrowatinexpath
加载,并通过prepareforsgue: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.lojavalueforkey:@“endereco”]
我可以获得我想要的属性,但这样我相信我没有使用CoreData的资源。您得到的错误是因为self.loja
是一个字典:在didselectrowatinexpath
中,您有:
self.loja = [self.listaFiltrada objectAtIndex:indexPath.row];
它是从过滤后的plist派生的NSDictionary,而不是在cellforrowatinexpath
中创建的NSManagedObject
。字典没有实现endereco
方法,因此出现错误。它确实实现了valueForKey
,因此这将起作用:但是如果您遵循此路径,则访问的是字典的键,而不是NSManagedObject
我认为您应该重新考虑您的方法-使用
cellforrowatinexpath
将plist处理为NSManagedObject
s是个坏主意。它只会对可见单元格调用,并且可能会对同一行调用多次(如果在屏幕上和屏幕下滚动)。我将编写一个例程,将所有plist处理为核心数据,并生成一个NSManagedObject
s数组,然后您可以将其用作表视图的数据源。endereco
看起来像是NSDictionary
的键。。。您是否尝试过使用request.naturalLanguageQuery=self.loja[@“endereco”]代码>?或者<代码>request.naturalLanguageQuery=[self.loja objectForKey:@“endereco”]代码>?我还是没有试过那样做。考虑到实体已充电且属性已填充,我认为执行self.entity.attribute
将有效。即使这样,[self.entity valueForKey:@“attribute”]
使用CoreData的“正确方法”是吗?是的,这正是我在考虑使用CoreData的方法,但我不知道如何实现。您可以说创建一个方法来读取plist并返回NSManagedObject。因此,在方法cellForRowAtIndexPath中,获取此方法,该方法返回NSManagedObject作为填充单元格的数据;这是一个一次性操作(除非plist改变?)。因此,您可以向应用程序委托添加一个方法来执行此操作。然后,在表视图的viewdiload
中,可以将所有NSManagedObject提取到一个数组中(可以包含一个谓词来过滤它们以匹配txtCategoria
)。然后在cellforrowatinexpath
中使用该数组。或者您可以使用NSFetchedResultsController
获取对象。plist文件不会更改。我是否可以创建自己的方法,或者需要将转换放入didfishlaunchingwithoptions
method?通过这样做,当我将NSManagedObjects
提取到一个数组中时,我不会陷入当前使用的相同概念,对吗?我会添加您自己的方法将plist加载到CoreData中,并从didfishlaunchingwithoptions:
调用它。然后在表视图控制器中,根本不需要引用plist;您只需获取NSManagedObjects—这应该可以避免您获得的特定错误,但也为表视图提供了一个健壮的数据源。
self.loja = [self.listaFiltrada objectAtIndex:indexPath.row];