【问题标题】:NSPredicate for filter objects whoes string is BEGINSWITH special charactersNSPredicate 用于过滤对象的强项是 BEGINS WITH special characters
【发布时间】:2016-12-27 07:39:19
【问题描述】:

我有一种情况,我想过滤具有NSDictionary 对象的NSArray 对象,并且每个对象都有键name。我想用name 键值应以特殊字符或数字字符(如0-9,~!@#$%^&*()_ 等)开头的对象数量进行过滤。

我试图在谷歌上找到这个但没有得到正确的解决方案。我使用了以下谓词,但无法获得正确的对象。

NSPredicate *predicate=   [NSPredicate predicateWithFormat:@"self.name BEGINSWITH %@",@"[^0-9]+.*"];

【问题讨论】:

    标签: ios objective-c nsarray nspredicate nscompoundpredicate


    【解决方案1】:

    BEGINSWITH 运算符不支持正则表达式。您可以使用谓词中的以下内容检查它是否以非字母字符开头:

     NSString *myRegex = @"[A-Za-z]*";
     NSPredicate *myTestPred = [NSPredicate predicateWithFormat:@"NOT (SELF MATCHES %@)", myRegex]
    

    【讨论】:

    • 不工作..它在运行时崩溃并出现异常。 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "NOT (SELF MATCHES[c] '^[A-Za-z].*'"'
    • 我在解决谓词中的语法错误后尝试过,但仍然无法正常工作
    • 是的,它现在可以工作,但我认为regex 应该是这个@"^[A-Za-z]*".. 但它可以工作.. 再次感谢。
    猜你喜欢
    • 2022-03-26
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2022-12-01
    相关资源
    最近更新 更多