0

我正在尝试过滤这样的对象数组:

{
    "id": "0",
    "title": "Crucifix and sarcophagus of Ariberto da Intimiano",
    "categoryName": "COMMEMORATIVE MONUMENTS",
    "audioFile": "Crucifix-and-sarcophagus-of-Ariberto-da-Intimiano",
    "videoFile": null,
    "textFile": "Crucifix-and-sarcophagus-of-Ariberto-da-Intimiano",
    "image": "content/0/Crucifix-and-sarcophagus-of-Ariberto-da-Intimiano_flow.jpg",
    "numPictures": "1",
    "didascalie": ["dida 1", "dida 2", "dida 3"],
    "description": "Crucifix and sarcophagus of Ariberto da Intimiano",
    "link": ""
},
{
    "id": "1",
    "title": "The sundial of Duomo",
    "categoryName": "THE SUNDIAL",
    "audioFile": "The-sundial-of-Duomo",
    "videoFile": null,
    "textFile": "The-sundial-of-Duomo",
    "image": "content/1/The-sundial-of-Duomo_flow.jpg",
    "numPictures": "2",
    "didascalie": ["dida 1", "dida 2", "dida 3"],
    "description": "The sundial of Duomo",
    "link": ""
}


NSPredicate *key1Predicate = [NSPredicate predicateWithFormat:@"title == '%@'",_searchAZ.text];

    _ArtWorksTableResults = [[_ArtWorksTable filteredArrayUsingPredicate:key1Predicate] mutableCopy];

    NSLog(@"%i",_ArtWorksTableResults.count);

当我执行搜索时,_ArtWorksTableResults.count总是0

但是,如果我编写谓词而不是像这样使用搜索栏文本:

NSPredicate *key1Predicate = [NSPredicate predicateWithFormat:@"title == 'The sundial of duomo'"];

    _ArtWorksTableResults = [[_ArtWorksTable filteredArrayUsingPredicate:key1Predicate] mutableCopy];

    NSLog(@"%i",_ArtWorksTableResults.count);

_ArtWorksTableResults.count是,这1是正确的!

我做错了什么?

我也尝试记录搜索栏文本,一切正常。

我不明白为什么如果我直接在代码中写搜索键就可以了!

4

2 回答 2

0

提供参数时,谓词格式中不应包含引号:

[NSPredicate predicateWithFormat:@"title == %@",_searchAZ.text];
于 2013-08-11T09:51:20.343 回答
0

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html

不需要额外的 ' 符号,它们由格式化程序添加

在最后:

@"%K == '%@'"
This predicate checks whether the value of the key %K is equal to the string literal “%@“ (note the single quotes around %@). The key name %K is supplied at runtime as an argument to predicateWithFormat:.
于 2013-08-11T09:53:41.093 回答