nspredicate iphone 问题

发布于 2024-11-27 20:34:01 字数 378 浏览 0 评论 0原文

我有以下代码,

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"Word== %@", randomWord];
rows = [[courseArray filteredArrayUsingPredicate:predicate]retain];

我在搜索栏中使用它。我有两个问题 - 如何制作 NSPredicate 以便它接受以键入的字符开头的每个单词,而不是使其值等于另一个?例如,我的数组中有“Men,Many,Morons,Now,Never”,当我输入“M”时,我希望添加“Men,Many,Morons”。此外,我如何谓词超过 1 个值?那么它比较两种格式?

谢谢...

i have the following code

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"Word== %@", randomWord];
rows = [[courseArray filteredArrayUsingPredicate:predicate]retain];

I am using this in a search bar. I have two questions - how can i make the NSPredicate so it takes every word that begins with the characters typed rather than having a value equal another? e.g I have "Men, Many,Morons, Now,Never" in the array, and when i type "M" i want "Men,Many,Morons" to be added. Furthermore, how can i predicate more than 1 value? so it compares between two formats?

thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰之心 2024-12-04 20:34:01

尝试
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Word BEGINSWITH %@", randomWord];

如果你想搜索整个单词,你可以尝试CONTAINS..

Try
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Word BEGINSWITH %@", randomWord];

If you want to search the whole word you can try CONTAINS..

痴情换悲伤 2024-12-04 20:34:01

NSPredicate文档是一个很好的起点。

检查字符串是否以另一个字符串开头的谓词是 [NSPredicate predicateWithFormat:@"Word BEGINSWITH %@"]

并且就检查两个谓词而言,您正在寻找 NSCompoundPredicate

The NSPredicate documentation is a good place to start.

The predicate to check if the string begins with another is [NSPredicate predicateWithFormat:@"Word BEGINSWITH %@"]

And as far as checking two predicates, you're looking for NSCompoundPredicate.

眸中客 2024-12-04 20:34:01

您需要创建要比较的所有谓词并将所有谓词附加到数组。之后,您可以使用 NSCompoundPredicate 作为 orand 条件。

像那样

NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"is_neutered = 0"],
    [NSCompoundPredicate orPredicateWithSubPredicates:feedDays]]];

You need create all of the predicate which you want to compare and append all predicate to array. after that you can use NSCompoundPredicate for or,and condition.

like that

NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"is_neutered = 0"],
    [NSCompoundPredicate orPredicateWithSubPredicates:feedDays]]];
梦罢 2024-12-04 20:34:01

http://developer.apple.com/ Library/ios/#documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html

在字符串常量、变量和字符串下查找“前缀”部分通配符部分。

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html

Look for the 'Prefix' part under the String Constants, Variables, and Wildcards section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文