stringWithFormat:与predicateWithFormat:

时间:2016-03-24 15:09:42

标签: ios objective-c cocoa nspredicate

使用from __future__ import print_function xs = [] for i in xrange(1, 11, 2): xs.append(i) print(*xs) 时,我希望尽可能重复使用我的论点。像这样:

NSString stringWithFormat:

使用NSPredicate这不起作用:

NSString *s = [NSString stringWithFormat:@"%1$@ = %2$@ OR (%1$@ = %2$@ AND %3$@ = %4$@)",
                  someValue, @YES,
                  anotherValue, @NO];

我需要做的是:

NSPredicate *p = [NSPredicate predicateWithFormat:@"%1$K = %2$@ OR (%1$K = %2$@ AND %3$K = %4$@)",
                              someKey, @YES,
                              anotherKey, @NO];

我知道NSPredicate仅支持%K(predicateWithFormat vs stringWithFormat: is the former supposed to do the same as the latter?)并且存在一些差异,但输入两次仍然很难看。还有其他选择吗?

0 个答案:

没有答案