How to check object is nil or not in swift?

后端 未结 11 2066
借酒劲吻你
借酒劲吻你 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:11

    The null check is really done nice with guard keyword in swift. It improves the code readability and the scope of the variables are still available after the nil checks if you want to use them.

    func setXYX -> Void{
    
         guard a != nil  else {
            return;
        }
    
        guard  b != nil else {
            return;
        }
    
        print (" a and b is not null");
    }
    

提交回复
热议问题