Please Help Me Intepret This Code SWIFT

前端 未结 4 580
遥遥无期
遥遥无期 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:33

    The answers about the ternary operator are correct.

    An alternative way to write this would be with the "nil-coalescing operator" ??:

    display.text = newValue ?? ""
    

    Which means if the value before ?? Is not nil, use that unwrapped value, else use the value after ??

提交回复
热议问题