nspredicate iphone 问题
我有以下代码,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
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
..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
.您需要创建要比较的所有谓词并将所有谓词附加到数组。之后,您可以使用
NSCompoundPredicate
作为or
、and
条件。像那样
You need create all of the predicate which you want to compare and append all predicate to array. after that you can use
NSCompoundPredicate
foror
,and
condition.like that
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.