What does the question mark and the colon (?: ternary operator) mean in objective-c?

前端 未结 13 2412
南旧
南旧 2020-11-22 04:10

What does this line of code mean?

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

The ? and :

13条回答
  •  遥遥无期
    2020-11-22 04:22

    As everyone referred that, It is a way of representing conditional operator

    if (condition){ 
        true 
    } 
    else {
        false
    }
    

    using ternary operator (condition)? true:false To add additional information, In swift we have new way of representing it using ??.

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

    Which is similar to

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

    is equivalent to

    if (a>c)?a:c ==> Is equal to if (a>c)?:c

    instead of ?: we can use ?? is swift.

提交回复
热议问题