Skip to content

Instantly share code, notes, and snippets.

@jjacobson93
Last active March 14, 2017 20:57
Show Gist options
  • Save jjacobson93/afa89274612fd78855794b86bceb844e to your computer and use it in GitHub Desktop.
Save jjacobson93/afa89274612fd78855794b86bceb844e to your computer and use it in GitHub Desktop.
Swift Guard-let Statement
func giveMeAStringMaybe(_ maybe: Bool) -> String? {
return maybe ? "Yes!" : nil
}
let str: String? = giveMeAStringMaybe(true)
guard let unwrappedStr = str else {
print("str is nil")
// unwrappedStr is NOT accessible here
return
}
// unwrappedStr is accessible here
print(unwrappedStr) // prints: "Yes!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment