How do you use the Optional variable in a ternary conditional operator?

前端 未结 6 1516
深忆病人
深忆病人 2021-01-30 10:23

I want to use an Optional variable with the ternary conditional operator but it is throwing error this error: optional cannot be used as boolean. What am I doing wrong?

6条回答
  •  情深已故
    2021-01-30 10:42

    This even works well if the value you want is a property or result of a function call on an optional (in Swift 3.0):

    return peripheral?.connected ?? false
    

提交回复
热议问题