简体   繁体   English

NSPredicate的异常行为,

[英]Strange behavior of NSPredicate,

Why does this predicate returns results: 为什么该谓词返回结果:

predicate= [NSPredicate predicateWithFormat:@"itemsString like '*{4160366}*'"];

while this predicate returns empty array 而此谓词返回空数组

predicate= [NSPredicate predicateWithFormat:@"itemsString like '*{%@}*'",[NSString stringWithString:@"4160366"]];

I's driving me crazy 我让我发疯

When building a predicate, predicateWithFormat automatically adds quotes when performing variable substitution. 构建谓词时,predicateWithFormat在执行变量替换时会自动添加引号。 So your (second) predicate ends up looking like this: 因此,您的(第二个)谓词最终看起来像这样:

itemsString like '*{"4160366"}*'" . itemsString like '*{"4160366"}*'"

Notice the extra set of quotes. 请注意额外的引号集。

Change it to: 更改为:

predicate= [NSPredicate predicateWithFormat:@"itemsString like %@",[NSString stringWithFormat:@"*{%@}*", @"4160366"]];

and it should work. 它应该工作。

(Note that instead of using stringWithString I just used @"" which does the same thing.) (请注意,不是使用stringWithString而是使用@""来完成相同的操作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM