用谓词查询核心数据 - iPhone [英] Querying Core Data with Predicates - iPhone

查看:100
本文介绍了用谓词查询核心数据 - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试从核心数据中获取对象。我有80个对象的列表,我希望能够使用UISearchBar搜索它们。它们显示在表格中。

So I'm trying to fetch objects from core data. I have list of say 80 objects, and I want to be able to search through them using a UISearchBar. They are displayed in a table.

在谓词上使用apple文档,我将以下代码放在UISearchBar委托方法的一个
中。

Using the apple documentation on predicates, I've put the following code in one of the UISearchBar delegate methods.

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  if (self.searchBar.text !=nil)
  {
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name LIKE %@", self.searchBar.text];
    [fetchedResultsController.fetchRequest setPredicate:predicate];
  }
  else
  {
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"All"];
    [fetchedResultsController.fetchRequest setPredicate:predicate];
  }

  NSError *error = nil;
  if (![[self fetchedResultsController] performFetch:&error]) {
        // Handle error
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();  // Fail
    }       

  [self.tableView reloadData];

    [searchBar resignFirstResponder];   
    [_shadeView setAlpha:0.0f];
}

如果我在搜索字段中输入与一个名称属性的完全匹配在这些对象中,搜索工作,并使用具有对象名称的单个单元格重新填充表。如果我不搜索确切的名称,我最终没有结果。

If I type in the search field an exact match to the name property of one of those objects, the search works, and it repopulates the table with a single cell with the name of the object. If I don't search the name exact, I end up with no results.

有什么想法吗?

推荐答案

好像iPhone似乎并没有喜欢LIKE运算符。我用'contains [cd]'替换它,它按照我想要的方式工作。

It seems as though iPhone doesn't like the LIKE operator. I replaced it with 'contains[cd]' and it works the way I want it to.

这篇关于用谓词查询核心数据 - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆