Using the Swift if let with logical AND operator &&

前端 未结 6 464
名媛妹妹
名媛妹妹 2021-01-30 19:13

We know that we can use an if let statement as a shorthand to check for an optional nil then unwrap.

However, I want to combine that with another expression

6条回答
  •  醉梦人生
    2021-01-30 20:05

    Swift 4, I will use,

    let i = navigationController?.viewControllers.index(of: self)
    if let index = i, index > 0, let parent = navigationController?.viewControllers[index-1] {
        // access parent
    }
    

提交回复
热议问题