I am getting into swift for the first time here and I have stumbled across a closure statement that doesn't make a whole lot sense to me based off of my current understanding of how closures are written. This is really a two-part question because I also don't quite grasp the intention/interpretation behind the conditional binding with this closure.
The chunk of code I am thrown by is this:
FIRAuth.auth()?.createUser(withEmail: email, password: password) {
(user, error) in if let error = error {
print(error.localizedDescription)
return
}
}
My understanding is that the closure needs specify a return type based off of the docs definition (something1, something2) -> ()
so from the bit of code above, does that just mean that swift can infer a void return by not including the -> ()
?
My assumption is that the conditional binding statement just says 'if an error arg is passed into this closure, then print the error?
Please use as much detail as possible in your explanation as possible so I can further my understanding. Cheers!
The following are all equivalent
If no return type is supplied to a function(/closure), then the empty tuple type (which is typealiased by
Void
) is inferred. We may, however, explicitly supply this return type, either as()
(the type), orVoid
. From the language guide - functions:Conversely, if no
return
is given at the end of a function block, it will be the same as explicitly returning an instance of the empty tuple, namely()
(the value). This may also be written simply asreturn
.