shle2821
shle2821

Reputation: 1886

In Swift, can you pass nil as an input to a function?

I'm reading many articles about how you shouldn't check an object for nil. It's a objC paradigm and it's a bad design and w/ swift it's been eliminated. So my question is, per example below, can you pass thru "group" as nil value? does the nil-checking mechanism happen when the function is called, hence removing the need to implement if(group==nil){..} ?

func deleteMembershipForGroup(group:GroupData){
}

Upvotes: 3

Views: 8282

Answers (2)

Jeff Yeung Sam Wai
Jeff Yeung Sam Wai

Reputation: 51

Yes! Thomas Kilian is right and it works for me! You will then be able to pass a nil parameter. You will also notice that using optional variable, it will also removed the warning saying the variable "group" will always be true.

func deleteMembershipForGroup(group:GroupData?){
   if let groupReal = group { <--- Warning gone!
   // not nil
   }
}

Upvotes: 2

qwerty_so
qwerty_so

Reputation: 36313

You need to use an optional:

func deleteMembershipForGroup(group:GroupData?){
  if let groupReal = group {
  // not nil
  }
}

Upvotes: 14

Related Questions