NSPredicate包含字符串小写

Van*_*nel 18 core-data nspredicate ios

我正在使用iOS SDK 6.0和XCode 4.5.2开发iOS应用程序.我的目标发展是4.3.

我正在使用Core Data来管理我的数据.现在我有这个NSPredicate搜索商店:

if ((shopSearchBar.text != nil) && ([shopSearchBar.text length] > 0))
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",shopSearchBar.text];
    [fetchRequest setPredicate:predicate];
}
Run Code Online (Sandbox Code Playgroud)

这是Shop实体:

在此输入图像描述

我必须将名称转换为小写,看看它是否包含shopSearchBar.text小写格式.

例:

我有这四家店:

  • Shop1
  • 商店1
  • 我的店铺

如果用户搜索文本是"shop",则必须返回所有文本.

你知道怎么做吗?

Van*_*nel 62

这就是我解决问题的方法:

if ((shopSearchBar.text != nil) && ([shopSearchBar.text length] > 0))
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@",
                              shopSearchBar.text];
    [fetchRequest setPredicate:predicate];
}
Run Code Online (Sandbox Code Playgroud)

  • 你不需要为_nil_检查`text`.只写`if(shopSearchbar.text.length)`,在两种情况下都会返回_nil_,文本为nil或空字符串. (3认同)