Please Help Me Intepret This Code SWIFT

前端 未结 4 568
遥遥无期
遥遥无期 2020-12-07 05:35
display.text = newValue != nil ? \"\\(newValue!)\" : \" \" 

Does the syntax of the code mean, let display.text = newValue, if it does not equal nil

4条回答
  •  北海茫月
    2020-12-07 06:31

    From the documentation

    Ternary Conditional Operator

    The ternary conditional operator is a special operator with three parts, which takes the form question ? answer1 : answer2. It is a shortcut for evaluating one of two expressions based on whether question is true or false. If question is true, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value.

    The ternary conditional operator is shorthand for the code below:

    if question {
       answer1 
    } else {
       answer2
    }
    

提交回复
热议问题