Created
July 31, 2016 13:55
-
-
Save amlcurran/a2597e3888f79b501aa301c9c82c8fd4 to your computer and use it in GitHub Desktop.
if let and guard let
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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