2

我有一个非常简单的 NSPredicate :

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"name beginswith '%@'", theString];
[matchingTags filterUsingPredicate:sPredicate];

当 theString == "p" 时,这会导致数组有 0 个结果

但是,当我这样做时:

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"name beginswith 'p'"];
[matchingTags filterUsingPredicate:sPredicate];

正如预期的那样,我得到了 100 多个结果。

我用 NSLog() 检查了“theString”,它的值是正确的。我觉得我错过了一些关键的秘密。可能是因为我使用的是字符串而不是字符?

想法?

4

1 回答 1

4

在此处查看文档

如果您使用 %@ 进行变量替换(例如 firstName like %@),则会自动为您添加引号。

所以基本上,它正在寻找以“p”而不是p开头的名称。将您的代码更改为:

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"name beginswith %@", theString];

应该管用

于 2010-06-28T17:03:20.633 回答