Skip to content

Instantly share code, notes, and snippets.

@amlcurran
Created July 31, 2016 13:55
Show Gist options
  • Save amlcurran/a2597e3888f79b501aa301c9c82c8fd4 to your computer and use it in GitHub Desktop.
Save amlcurran/a2597e3888f79b501aa301c9c82c8fd4 to your computer and use it in GitHub Desktop.
if let and guard let
func doSomething(optionalString: String?) {
if let string = optionalString {
let count = string.utf8.count // string is not optional and can be used normally
}
}
func doSomething(optionalString: String?) {
guard let string = optionalString else {
// this closure is called if optionalString was nil
return
}
// string is not optional for the rest of the function
let count = string.utf8.count
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment