我在搜索如何选择核心数据中实体中特定属性的所有值。在实体核心数据中选择特定属性
这里我的模型:
我有一个名为 “国家”,类型为 “字符串” 的3个属性的实体:
- name_en
- name_fr
- name_de
我在UITableView上显示值,但我只想显示来自name_en或name_fr或name_de的值在用户默认语言。目前,所有的值都被选中(name_en,name_fr和name_de)。
解决方案(感谢Simon):
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K MATCHES %@", [NSString stringWithFormat:@"name_%@",[Config getCurrentLocale]], @".{1,}"];
当[配置getCurrentLocale]返回我 “FR” 或 “德” 或 “EN”。
接下来,应用此谓词来请求:
NSFetchRequest * request = [self defaultRequest];
request.predicate = predicate;
self.fetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:context
sectionNameKeyPath:nil
cacheName:nil];
=========相对于我的问题(现在已经解决了)==========
这里是我的方法,我建立了读取请求:
-(NSFetchRequest *) defaultRequest
{
NSFetchRequest * request = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription entityForName:self.entityName
inManagedObjectContext:appDelegate.managedObjectContext];
NSDictionary *attributes = [entity attributesByName];
// Put some stuff here to "filter" the request and show only name_en OR name_fr OR name_de ?
request.entity = entity;
request.fetchBatchSize = 20;
//request.fetchLimit = 50;
request.sortDescriptors = self.defaultSortDescriptors;
return request;
}
谢谢你们的帮助。
编辑1
这里是我的UITableViewController的子类的数据源的方法:
#pragma mark - UITableView
- (NSFetchedResultsController *)fetchedResultsControllerForTableView:(UITableView *)tableView
{
if (tableView == self.searchDisplayController.searchResultsTableView)
return self.searchFetchedResultsController;
if (tableView == self.tableView)
return self.fetchedResultsController;
return nil;
}
#pragma mark DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[[self fetchedResultsControllerForTableView:tableView] sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[[self fetchedResultsControllerForTableView:tableView] sections] objectAtIndex:section] numberOfObjects];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[[self fetchedResultsControllerForTableView:tableView] sections] objectAtIndex:section] name];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [[self fetchedResultsControllerForTableView:tableView] sectionForSectionIndexTitle:title atIndex:index];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [[self fetchedResultsControllerForTableView:tableView] sectionIndexTitles];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * identifier = [self cellIdentifierForIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
cell = [self newTableViewCellWithIndentifier:identifier];
// Configure the cell.
[self tableView:tableView configureCell:cell atIndexPath:indexPath];
return cell;
}
编辑2:
我尝试这样的事情只是为了显示结果输出:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Countries"];
fetchRequest.resultType = NSDictionaryResultType;
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"name_en", nil]];
NSError *error = nil;
NSArray *results = [[AppDelegate sharedInstance].managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSMutableArray *countries = [results valueForKey:@"name_en"];
NSLog(@"Count: %d", countries.count);
通常,我在名称中有大约246个国家,计数为757!因此,name_fr的总和+ name_en + name_de ......我不明白为什么:(
编辑=>我发现了一个解决方案在这里:How to fetch unique field values (song_type) from core data
现在,我已经适应我的代码与作品这段代码:)
谢谢!我想我已经尝试过了,但没有奏效。我会再试一次,让它知道它是否有效;) – Lapinou
嗯,它确实有效。但我的问题是,在我的UITableView中,我有来自name_fr,name_de和name_nl的所有值,但唯一显示的值是正确的。我的意思是,如果我将de属性设置为“name_en”,显示的值是“name_en”,但在我的UITableView中,单元格数等于“name_en + name_fr + name_nl值...”。我不明白为什么:/ – Lapinou
@Lapinou将代码粘贴到实现UITableViewDatasource委托的方式上。没有得到你的意见 - 但在我的UITableView中,我的单元格数量等于“name_en + name_fr + name_nl值...” – Simon