Predicate Format String Syntax 与字面量

本文探讨了Objective-C中NSPredicate的格式化字符串使用方法,特别是%@和%K变量替换符的作用及注意事项。解释了如何正确使用单引号、双引号以及变量替换来构建有效的谓词。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字面量:

字符串:单引号或双引号扩起来;

%@:系统自动添加,数字则自动去除@();

无单双引号,系统做数字处理。

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.

系统自动添加   double quoting

 NSPredicate *predicate   = [NSPredicate predicateWithFormat:@"%K = %@", @"a", @"ddd"];

    NSPredicate *predicatex   = [NSPredicate predicateWithFormat:@"eee = %@", @"ddd"];

    NSPredicate *predicateh   = [NSPredicate predicateWithFormat:@"h = 'hhh'"];

    NSPredicate *predicatee   = [NSPredicate predicateWithFormat:@"%K = %@",@"c", @(7)];

    NSPredicate *predicatep   = [NSPredicate predicateWithFormat:@"%K = %d",@"e", 7];

    NSLog(@"\r\n%@\r\n%@\r\n%@\r\n%@\r\n%@",predicate,predicatex,predicateh,predicatee,predicatep);

a == "ddd"

eee == "ddd"

h == "hhh"

c == 7

e == 7

 

NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"appId == '%@'", [FQAccountManager sharedFQAccountManager].appId]];

属于二次转化。

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html#//apple_ref/doc/uid/TP40001795-SW1

Parser Basics

The predicate string parser is whitespace insensitive, case insensitive with respect to keywords, and supports nested parenthetical expressions. The parser does not perform semantic type checking. 

Variables are denoted with a dollar-sign ($) character (for example, $VARIABLE_NAME). The question mark (?) character is not a valid parser token. 

The format string supports printf-style format specifiers such as %x (see Formatting String Objects). Two important format specifiers are %@ and %K.

  • %@ 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 string using the %@ format specifier, 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.

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"

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 to firstName like "%@" (note the single quotes around %@).

NSString *attributeName = @"firstName";
NSString *attributeValue = @"Adam";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like '%@'",
        attributeName, attributeValue];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值