这行代码是什么意思?

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

的吗?还有,迷惑我吧。


当前回答

三元运算符示例。如果值为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。

其他回答

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

condition ? valueIfTrue : valueIfFalse

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

这只是if-then-else语句的一种简短形式。它的意思与下面的代码相同:

if(inPseudoEditMode)
  label.frame = kLabelIndentedRect;
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:

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

简单地说,逻辑是

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

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

a?b:c

就像

if(a)
    b;
else
    c;