简体   繁体   English

什么是?:运算符

[英]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等于0a 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.

相关问题 ^运算符对BOOL做了什么? - What does the ^ operator do to a BOOL? '-&gt;'(箭头运算符)和'.'有什么区别? (点运算符)在 Objective-C 中? - What is the difference between '->' (arrow operator) and '.' (dot operator) in Objective-C? Objective C中isEqualToString和operator ==之间的差异是什么? - What is the diffrence between isEqualToString and operator == in Objective C? obj c函数中(+)和(-)运算符有什么区别 - What is the difference between (+) and (-) operator in obj c function 什么是使用条件运算符的其他什么都不做? - What is the equivalent of else do nothing using the conditional operator? 在Objective-C中使用三元运算符有什么限制? - What are the limits of using a ternary operator in Objective-C? 对于返回void的函数,什么都不做运算符是什么? - What would be a do nothing operator for a function returning void? 在这种情况下,“ =”运算符是做什么的? sqrtf(1-(t = t / d-1)* t); - What does the '=' operator do in this context? sqrtf(1 - (t = t / d - 1) * t); Leopard似乎不支持运算符@“%@ == [c]%@”。 那我该怎么用呢? - the operator @“%@ ==[c] %@” seems not supported on Leopard. What should I use then? 使用什么类型的变量来存储数学运算符? - What type of variable do I use to store mathematical operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM