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

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

    In case the comparison is based on some condition

     let sliderValue = Float(self.preferenceData.getLocationRadius().characters.count > 1 ?self.preferenceData.getLocationRadius():"0.1")
    

    Here the function getLocationRadius() returns a String. One more thing if we don't put a space between 1 and ? it results in an syntax error

提交回复
热议问题