Ios stringWithFormat:vs.predicateWithFormat:
使用Ios stringWithFormat:vs.predicateWithFormat:,ios,objective-c,cocoa,nspredicate,Ios,Objective C,Cocoa,Nspredicate,使用NSString stringWithFormat:时,我喜欢尽可能重复使用参数。像这样: NSString *s = [NSString stringWithFormat:@"%1$@ = %2$@ OR (%1$@ = %2$@ AND %3$@ = %4$@)", someValue, @YES, anotherValue, @NO]; 使用NSPredicate无法执行此操作: NSPredicate *p =
NSString stringWithFormat:
时,我喜欢尽可能重复使用参数。像这样:
NSString *s = [NSString stringWithFormat:@"%1$@ = %2$@ OR (%1$@ = %2$@ AND %3$@ = %4$@)",
someValue, @YES,
anotherValue, @NO];
使用NSPredicate无法执行此操作:
NSPredicate *p = [NSPredicate predicateWithFormat:@"%1$K = %2$@ OR (%1$K = %2$@ AND %3$K = %4$@)",
someKey, @YES,
anotherKey, @NO];
我要做的是:
NSPredicate *p = [NSPredicate predicateWithFormat:@"%K = %@ OR (%K = %@ AND %K = %@)",
someKey, @YES,
someKey, @YES,
anotherKey, @NO];
我知道NSPredicate只支持%K(),并且存在一些差异,但是键入两次仍然很难看。还有其他选项吗?有点不相关,但在谓词(
(A或(A和B))
中重复检查%1$K=%2$@
的原因是什么。%1$K=%2$@
将计算为YES
,条件的第二部分将根本不计算,或者它将始终计算为NO
。我遗漏了什么吗?谢谢@BorisVerebsky,我无法使用原始代码。这是示例中的一个逻辑缺口。我只是想展示一下大概的想法。有点不相关,但在谓词((A或(A和B))
中重复检查%1$K=%2$@
的原因是什么。%1$K=%2$@
将计算为YES
,条件的第二部分将根本不计算,或者它将始终计算为NO
。我遗漏了什么吗?谢谢@BorisVerebsky,我无法使用原始代码。这是示例中的一个逻辑缺口。我只是想展示一下大概的想法。