Last active
March 14, 2017 20:57
-
-
Save jjacobson93/fd784fee6dfffa3d3623b46aa6e53832 to your computer and use it in GitHub Desktop.
Swift If-let Statement
This file contains 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 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