这行代码是什么意思?

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

的吗?还有,迷惑我吧。


当前回答

这是C的一部分,不是Objective-C特有的。下面是if语句的翻译:

if (inPseudoEditMode)
    label.frame = kLabelIndentedRec;
else
    label.frame = kLabelRect;

其他回答

它是三元或条件运算符。它的基本形式是:

condition ? valueIfTrue : valueIfFalse

其中只有在选中值时才会计算值。

这是C的一部分,不是Objective-C特有的。下面是if语句的翻译:

if (inPseudoEditMode)
    label.frame = kLabelIndentedRec;
else
    label.frame = kLabelRect;

正如大家所说,它是一种表示条件运算符的方法

if (condition){ 
    true 
} 
else {
    false
}

使用三元运算符(条件)?真:假 为了增加额外的信息,在swift中我们有新的方法来表示它使用??

let imageObject: UIImage = (UIImage(named: "ImageName")) ?? (initialOfUsername.capitalizedString).imageFromString

这和

int a = 6, c= 5;
if (a > c) 
{ 
 a is greater
} else {
 c is greater
}

等于

如果(> c) ?a:c ==> = if (a>c)?c:

我们可以用??是迅速的。

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

意味着

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

简单地说,逻辑是

(条件)?{YES代码}:{NO代码}