How to check object is nil or not in swift?

后端 未结 11 2031
借酒劲吻你
借酒劲吻你 2020-12-30 18:33

Suppose I have String like :

var abc : NSString = \"ABC\"

and I want to check that it is nil or not and for that I try :

if         


        
11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 19:30

    Swift 4 You cannot compare Any to nil.Because an optional can be nil and hence it always succeeds to true. The only way is to cast it to your desired object and compare it to nil.

    if (someone as? String) != nil
    {
       //your code`enter code here`
    }
    

提交回复
热议问题