5

我放弃。我已经尝试了所有我能想象到的组合来检查一个字符串是否包含另一个字符串。这是一个直观的语法示例,描述了我想要做什么:

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

不管我如何改变 NOT,使用 ! 运算符,而是移动括号或完全删除它们,我总是在解析这个表达式时遇到异常。

这个表达有什么问题?

编辑:当我打电话时发生异常

[pMetadataQuery setPredicate:pPredicate];

例外是:*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“NSMetadataQuery 的 NSComparisonPredicate 类型未知(kMDItemFSName CONTAINS[c]“Some String”)

4

2 回答 2

11

我在以下方面取得了圆满成功:

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. 遗憾的是我在这方面没有经验,但这肯定是接下来要研究的事情。

于 2012-07-13T19:06:05.497 回答
1

斯威夫特 3.0

let predicate = NSPredicate(format: "NOT (%K CONTAINS[c] %@)", "someKey", "Some String")
let testArray: [Any] = [[ "someKey" : "This sure is Some String" ], [ "someKey" : "I've nothing to say" ], [ "someOtherKey" : "I don't even have that key" ]]
let filteredArray: [Any] = testArray.filter { predicate.evaluate(with: $0) }
print("found \(filteredArray)")
于 2017-09-16T08:54:16.703 回答