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

前端 未结 6 1404
深忆病人
深忆病人 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条回答
  •  梦毁少年i
    2021-01-30 10:37

    Ternary operators operate on three targets. Like C, Swift has only one ternary operator, the ternary conditional operator (a ? b : c).

    Example usage on tableView -

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return section == 2 ?  4 :  1
    }
    

    indicates if section equal to 2 then it return 4 otherwise 1 on false.

提交回复
热议问题