Created
September 26, 2016 01:35
-
-
Save simonkim/967e2536e34f00633d226e1ff18f6aaf to your computer and use it in GitHub Desktop.
guard let vs. if nil
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
//: guard let example | |
func printifnonnil(text: String?) { | |
if text == nil { | |
return | |
} | |
// syntatically, unwrapping still required since text is still an optional | |
print(text!) | |
} | |
func printguard(text: String?) { | |
guard let text = text else { | |
return | |
} | |
// guranteed that text is not an optional | |
print(text) | |
} | |
printifnonnil(text: "hello") | |
printifnonnil(text: nil) | |
printguard(text: "hello") | |
printguard(text: nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment