使用NSPredicate检测NOT CONTAINS [英] Use an NSPredicate to detect NOT CONTAINS

查看:408
本文介绍了使用NSPredicate检测NOT CONTAINS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我放弃了。我试过每一个组合,我可以想象,以检查一个字符串是否包含另一个字符串。下面是一个描述我想要做的直观语法的例子:

I give up. I have tried every combination I can imagine to check if a string contains another string. Here's an example of intuitive syntax describing what I want to do:

    NSPredicate* pPredicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS[c] %@)",
NSMetadataItemFSNameKey, 
[NSString stringWithFormat:@"Some String"]];

无论我如何移动NOT,

Regardless of how I shift the NOT around, use the ! operator instead, shift the parentheses or remove them altogether, I always get an exception parsing this expression.

这个表达式有什么问题?

What is wrong with this expression?

编辑:当我调用

[pMetadataQuery setPredicate:pPredicate];

,例外情况是: * 由于未捕获异常而终止应用程序'NSInvalidArgumentException ',reason:'未知类型的NSComparisonPredicate给予NSMetadataQuery(kMDItemFSName CONTAINS [c]Some String)'

and the exception is: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unknown type of NSComparisonPredicate given to NSMetadataQuery (kMDItemFSName CONTAINS[c] "Some String")'

推荐答案

I完全成功:

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS[c] %@)",
        @"someKey",
        [NSString stringWithFormat:@"Some String"]];
NSArray *testArray =
    [NSArray arrayWithObjects:
        [NSDictionary dictionaryWithObject:@"This sure is Some String" forKey:@"someKey"],
        [NSDictionary dictionaryWithObject:@"I've nothing to say" forKey:@"someKey"],
        [NSDictionary dictionaryWithObject:@"I don't even have that key" forKey:@"someOtherKey"],
        nil];

NSArray *filteredArray = [testArray filteredArrayUsingPredicate:predicate];
NSLog(@"found %@", filteredArray);

testArray 结束于 filteredArray ,在OS X v10.7和iOS v4.3下。所以问题不是谓词 - 使这在技术上是一个完整的答案的问题 - 这是一种限制在 NSMetadataQuery 。可悲的是,我没有在这方面的经验,但它肯定是下一步研究。

The second two objects of the three in testArray ended up in filteredArray, under OS X v10.7 and iOS v4.3. So the issue isn't the predicate — making this technically a complete answer to the question — it's some sort of restriction in NSMetadataQuery. Sadly I've no experience in that area, but it's certainly the next thing to research.

这篇关于使用NSPredicate检测NOT CONTAINS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆