Swift3 optionals chaining in IF conditions bug?

后端 未结 4 673
感动是毒
感动是毒 2020-12-01 20:01

This code worked just fine in Swift 2.3 and I don\'t understand why I have to unwrap TestClass to check if number is bigger than 4. This is whole point of chain

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 20:59

    First of all, where are you initialising your test var? Of course it'll be nil if you don't give it a value!

    And regarding optional chaining, what's the issue writing :

    if let optionalInt = test?.optionalInt, optionalInt > 4
    {
    
    }
    

    As always, safety > brevity.

提交回复
热议问题