【问题标题】:nspredicate iphone questionnspredicate iphone问题
【发布时间】:2011-10-18 15:32:25
【问题描述】:

我有以下代码

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

我在搜索栏中使用它。我有两个问题 - 我怎样才能使 NSPredicate 使每个单词都以键入的字符开头,而不是使一个值等于另一个?例如,我在数组中有“Men, Many,Morons, Now,Never”,当我输入“M”时,我想要添加“Men,Many,Morons”。此外,我如何断言超过 1 个值?所以它在两种格式之间进行比较?

谢谢...

【问题讨论】:

    标签: objective-c xcode ios4 nspredicate


    【解决方案1】:

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

    这样

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多