这行代码是什么意思?

label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;

的吗?还有,迷惑我吧。


当前回答

int padding = ([[UIScreen mainScreen] bounds].size.height <= 480) ? 15 : 55;

意味着

int padding; 
if ([[UIScreen mainScreen] bounds].size.height <= 480)
  padding = 15;
else
  padding = 55; 

其他回答

三元运算符示例。如果值为isFemale 布尔变量是YES,打印“性别是女性”,否则打印“性别是” 男性”

? means = execute the codes before the : if the condition is true. 
: means = execute the codes after the : if the condition is false.

objective - c

BOOL是否是女性= YES; NSString * valuetopprint = (isFemale == YES) ?@“性别是女性”:@“性别是男性”; NSLog (valueToPrint);//结果将是"GENDER IS FEMALE",因为isFemale的值被设置为YES。

为迅速

let isFemale = false let valuetopprint:String = (isFemale == true) ?" gender is female ": " gender is male " print(valuetopprint) //Result将是"GENDER IS MALE",因为isFemale值被设置为false。

它是三元运算符,类似于if/else语句。

if(a > b) {
what to do;
}
else {
what to do;
}

在三元运算符中是这样的: 条件?如果条件为真怎么办:如果它为假怎么办;

(a > b) ? what to do if true : what to do if false;

我刚学了一些关于三元运算符的新东西。省略中间操作数的简短形式确实很优雅,这是C语言仍然重要的众多原因之一。仅供参考,我第一次真正了解这个是在c#实现的一个例程的上下文中,该例程也支持三元操作符。由于三元运算符是在C语言中,因此在其他本质上是它的扩展的语言中(例如,Objective-C, c#)也是如此。

基于Barry Wark的精彩解释……

三元操作符的重要之处在于它可以用于if-else不能使用的地方。即:在条件或方法参数内。

[NSString stringWithFormat: @"Status: %@", (statusBool ? @"Approved" : @"Rejected")]

...这是预处理常量的一个很好的用途:

// in your pch file...
#define statusString (statusBool ? @"Approved" : @"Rejected")

// in your m file...
[NSString stringWithFormat: @"Status: %@", statusString]

这使您不必在if-else模式中使用和释放局部变量。增值!

这就是常见的三元运算符。如果问号之前的部分为真,则计算并返回冒号之前的部分,否则计算并返回冒号之后的部分。

a?b:c

就像

if(a)
    b;
else
    c;