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

前端 未结 6 1403
深忆病人
深忆病人 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:50

    You can not assign string value to bool but You can check it str1 is nil or not like this way :

    myBool = str1 != nil ? true : false
    print(myBool)
    

    It will print false because str1 is empty.

提交回复
热议问题