Skip to content

Instantly share code, notes, and snippets.

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