objective-c – 列名称的NSPredicate问题

前端之家收集整理的这篇文章主要介绍了objective-c – 列名称的NSPredicate问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用NSPredicate查询表.这本质上是我在做什么
NSNumber *value = [NSNumber numberWithInteger: 2];
NSString *columnName = @"something_id";

NSLog(@"%@ == %@",columnName,value);
NSPredicate *refQuery = [NSPredicate predicateWithFormat: @"%@ == %@",value];

NSLog打印出我的期望(“something_id == 2”),但谓词不起作用.
但是,如果我将更改为:

NSPredicate *refQuery = [NSPredicate predicateWithFormat: @"something_id == %@",value];

那么为什么这不工作,我该怎么解决呢?

解决方法

The Predicate Programming Guide from Apple说:

%@ is a var arg substitution for an
object value—often a string,number,
or date.

%K is a var arg substitution
for a key path.

When string variables
are substituted into a format string
using %@,they are surrounded by
quotation marks. If you want to
specify a dynamic property name,use
%K in the format string.

所以,在你的情况下,你需要把%K作为一个关键字到columnName,而不是%@,它将被加上引号:

NSPredicate *refQuery = [NSPredicate predicateWithFormat: @"%K == %@",value];

希望这清楚你的疑惑.

原文链接:https://www.f2er.com/c/114517.html

猜你在找的C&C++相关文章