ios NSPredicate 包含字符串小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14611444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:03:52  来源:igfitidea点击:

NSPredicate contains string lowercase

ioscore-datanspredicate

提问by VansFannel

I'm developing an iOS application with iOS SDK 6.0 and XCode 4.5.2. My target development is 4.3.

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

I'm using Core Data to manage my data. Now I have this NSPredicateto search shops:

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

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

This is Shop entity:

这是商店实体:

enter image description here

在此处输入图片说明

I have to convert nameto lowercase and see if it contains shopSearchBar.textin lowercase format.

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

Example:

例子:

I have these four shops:

我有这四家店:

  • Shop1
  • shop 1
  • my shop
  • Shop
  • 店铺 1
  • 店铺 1
  • 我的店铺
  • 店铺

If user search text is 'shop', it must returns all of them.

如果用户搜索文本是“shop”,它必须返回所有这些。

Do you know how to do that?

你知道怎么做吗?

回答by VansFannel

This is how I've solved my problem:

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

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

回答by mjvotaw

You should be able to use NSPredicat's predicateWithBlock: method:

您应该能够使用 NSPredicat 的 predicateWithBlock: 方法:

NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id obj, NSDictionary *bind) {
return [self.name compare:shopSearchBar.text options:NSCaseInsensitiveSearch] == NSOrderedSame; }];

回答by deanWombourne

(a) You could add a column to your repository - lowercaseName - and every time you save a Shop, save a lowercase only version of it's name. Then your predicate is just compare :)

(a) 您可以在您的存储库中添加一列 - lowercaseName - 并且每次保存商店时,请保存其名称的仅小写版本。那么你的谓词只是比较:)

(b) However, if all you want is to do a case insensitive compare try this :

(b) 但是,如果您只想进行不区分大小写的比较,请尝试以下操作:

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

This is also diacritic insensitive compare. Look at the String Comparison section of this docs pagefor more options.

这也是变音符号不敏感比较。查看此文档页面的字符串比较部分以获取更多选项。

(a) gives you faster queries but more complicated code. (b) gives you very simple save methods but (slightly) slower queries

(a) 为您提供更快的查询但更复杂的代码。(b) 为您提供非常简单的保存方法,但(稍微)较慢的查询