objective-c NSPredicate 对多关系不区分大小写匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1473973/
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-09-03 22:15:09  来源:igfitidea点击:

NSPredicate case-insensitive matching on to-many relationship

objective-ccocoacore-data

提问by Brian Webster

I am implementing a search field where the user can type in a string to filter the items displayed in a view. Each object being displayed has a keywordsto-many relationship, and I would like to be able to filter the objects based on their keywords. Each keyword object has a nameproperty, so I've set up an NSPredicate to do the filtering that looks like this:

我正在实现一个搜索字段,用户可以在其中输入字符串来过滤视图中显示的项目。显示的每个对象都有keywords一对多的关系,我希望能够根据关键字过滤对象。每个关键字对象都有一个name属性,因此我设置了一个 NSPredicate 来执行如下过滤:

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"keywords.name CONTAINS %@", self.searchString];

This works, but the problem is that the search is case-sensitive, so if the keyword has a capital letter but the user types in all lowercase, no matches are found. I've tried the following modification:

这是可行的,但问题是搜索区分大小写,因此如果关键字有大写字母但用户输入全部小写,则找不到匹配项。我尝试了以下修改:

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"keywords.name CONTAINS[c] %@", self.searchString];

But that doesn't make any difference in the case sensitivity of the matching. Is there a way to do this case-insensitive matching using just a plain predicate? Or will I need to implement some sort of custom accessor on the keyword class, e.g. write a lowercaseNamemethod and match against a lowercased version of the search string instead?

但这对匹配的大小写敏感度没有任何区别。有没有办法只使用一个简单的谓词来进行这种不区分大小写的匹配?或者我是否需要在关键字类上实现某种自定义访问器,例如编写一个lowercaseName方法并匹配搜索字符串的小写版本?

Addendum: After further exploration, the workaround of adding a custom accessor works OK for manual use of NSPredicate, but does not work at all when using NSFetchRequest with Core Data, which only works when querying attributes defined in the Core Data model.

附录:经过进一步探索,添加自定义访问器的解决方法对于手动使用 NSPredicate 是可行的,但在将 NSFetchRequest 与 Core Data 一起使用时根本不起作用,它仅在查询 Core Data 模型中定义的属性时有效。

回答by Alfonso

If I understand you correctly, you want your predicate to be true whenever any keywords name matches the search string. For this you need to test with the ANY keyword like this:

如果我理解正确的话,你希望你的谓词在任何关键字名称与搜索字符串匹配时都为真。为此,您需要使用 ANY 关键字进行测试,如下所示:

[NSPredicate predicateWithFormat:@"ANY keywords.name CONTAINS[c] %@", ...];

This will search the keywords and return true if any of those keywords name contains your search string.

这将搜索关键字,如果这些关键字名称中的任何一个包含您的搜索字符串,则返回 true。

回答by slatvick

I believe the answer is:

我相信答案是:

[NSPredicate predicateWithFormat:@"keywords.name CONTAINS[cd] %@", self.searchString];

String comparisons are by default case and diacritic sensitive. You can modify an operator using the key characters c and d within square braces to specify case and diacritic insensitivity respectively, for example firstName BEGINSWITH[cd] $FIRST_NAME.

默认情况下,字符串比较区分大小写和变音符号。您可以使用方括号内的键字符 c 和 d 修改运算符以分别指定大小写和变音符号不敏感,例如 firstName BEGINSWITH[cd] $FIRST_NAME。

Predicate Format String Syntax

谓词格式字符串语法

回答by wal

If you is trying to catch only the equals names but with insensitive case, I think it is the best solution

如果您试图只捕获等于名称但不区分大小写,我认为这是最好的解决方案

[NSPredicate predicateWithFormat:@"ANY keywords.name LIKE[c] %@", ...];

You helped me a lot. Thanks guys!!!

你帮了我很多。谢谢你们!!!

In my case I did:

就我而言,我做了:

[NSPredicate predicateWithFormat:@"ANY name LIKE[c] %@", @"teste"];

回答by netshark1000

If you must match the keyword but the search must be case-insensitive then you should use NSPredicate(format: "keywords.name =[c] %@", self.searchString)

如果必须与关键字匹配,但搜索必须区分敏感,那么你应该使用NSPredicate(format: "keywords.name =[c] %@", self.searchString)

LIKEdoes not work on string literals.

LIKE不适用于字符串文字。

回答by Sven

If you want both case insensitive and wildcard, use this:

如果您想要不区分大小写和通配符,请使用以下命令:

NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(name like[c] '*%@*')",@"search"]];