NSPredicate 一些格式问题
The format string supports printf-style format arguments such as %x (see “Formatting String Objects”). Two important arguments are %@ and %K.
-
%@is a var arg substitution for an object value—often a string, number, or date. -
%Kis 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, as shown in the following example.
就是说%@ 会自动在 变量左右 加“”
%K 主要时用在 前面 表示 数据库里面的变量
NSString *attributeName = @"firstName"; |
NSString *attributeValue = @"Adam"; |
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@", |
attributeName, attributeValue]; |
The predicate format string in this case evaluates to firstName like "Adam".
The predicate format string in this case evaluates to firstName like "Adam".
Single or double quoting variables (or substitution variable strings) cause %@, %K, or $variable to be interpreted as a literal in the format string and so prevent any substitution. In the following example, the predicate format string evaluates tofirstName like "%@" (note the single quotes around %@).
NSString *attributeName = @"firstName"; |
NSString *attributeValue = @"Adam"; |
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like '%@'", |
attributeName, attributeValue]; |
浙公网安备 33010602011771号