[英]What is the ?: operator
In an example of Objective-C code I found this operator 在Objective-C代码的示例中,我找到了这个运算符
self.itemViews[@(0)] ?: [self.dataSource slidingViewStack:self viewForItemAtIndex:0 reusingView:[self dequeueItemView]];
The code does compile under Apple LLVM 4.2. 代码在Apple LLVM 4.2下编译。
The only thing I came across was in being a vector operator, but I don't think Objective-C, and for that matter C, has vector operators. 我遇到的唯一一件事就是成为一个向量运算符,但我不认为Objective-C,就C而言,它有向量运算符。 So can someone please give reference and or documentation of this operator.
那么有人可以提供此运营商的参考和/或文档。
?:
is the C conditional operator . ?:
是C 条件运算符 。
a ? b : c
yields b
value if a
is different than 0
and c
if a
is equal to 0
. 如果
a
不等于0
则产生b
值,如果a
等于0
则a
c
。
A GNU extension ( Conditional with Omitted Operand ) allows to use it with no second operand: GNU扩展( 带有省略操作数的条件 )允许在没有第二个操作数的情况下使用它:
x ? : y
is equivalent to 相当于
x ? x : y
Are you familiar with the Ternary operator ? 你熟悉三元运算符吗? Usually seen in the style:
通常在风格中看到:
test ? result_a : result_b;
All that has happened here is that the first branch has not been given so nothing will happen in the positive case. 这里发生的一切都是没有给出第一个分支,所以在正面情况下不会发生任何事情。 Similar to the following:
类似于以下内容:
test ?: result_b;
Due to the way that C is evaluated, this will return result_b
if test is untruthy, otherwise it will return test
. 由于C是评估的方式,这将返回
result_b
如果测试untruthy,否则将返回test
。
In the example that you have given - if the view is missing, it falls back to checking the datasource to provide a replacement value. 在您给出的示例中 - 如果缺少视图,则会回退到检查数据源以提供替换值。
The above is called Ternary operator or conditional operator. 以上称为三元运算符或条件运算符。
Syntax is, 语法是,
<condition>?<true_part>:<false_part>
Here if condition is true, will be considered as value else will be considered as value. 如果条件为真,则将其视为值,否则将被视为值。
Please refer this, http://en.wikipedia.org/wiki/%3F: 请参考http://en.wikipedia.org/wiki/%3F:
It is the ternary operator , as Objective-C is a superset of C you can use this operator. 它是三元运算符 ,因为Objective-C是C的超集,您可以使用此运算符。
Some tutorial on this. 关于此的一些教程 。
a = x ?
a = x? : y;
:y;
The expression is equivalent to 表达式相当于
a = x ?
a = x? x : y;
x:y;
It is the ? 它是 ? operator, called ternary operator, which is used in this way:
运算符,称为三元运算符,以这种方式使用:
condition ? true-branch : false-branch;
When condition evaluates to true (non zero), the branch before the :
is executed, otherwise the other branch is executed. 当条件求值为真(非零)时,执行
:
之前的分支,否则执行另一个分支。 This may even return a value: 这甚至可能返回一个值:
value = condition ? true-branch : false-branch;
In your case the return value is ommited and the true-branch is empty (nothing to do). 在你的情况下,返回值是ommited,真正的分支是空的(无事可做)。 The return value of
condition
is returned then but not used in your example. 然后返回
condition
的返回值,但不在您的示例中使用。
Equivalent of 相当于
if (!self.itemViews[@(0)])
[self.dataSource slidingViewStack:self viewForItemAtIndex:0 reusingView:[self dequeueItemView]];
which is imho much better programming style. 这是非常好的编程风格。
此运算符在Objective C中使用 ,此运算符用于条件运算符。是运行一个语句还是其他语句取决于所使用的逻辑术语和您提供的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.