Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
WTF is an Optional ?
var Cat:String? //<- the "?" is what just made Cat a optional
Cat = "Phil Knight"
let nowUnwrapped = Cat! //"!" <- How you can tell Swift to demand a real answer from the optional
// Swift -> what is your real name cat ?
// the technical term for this is called optional unwrapping
// Optional Binding - How you can check that your optional actually holds somthing before you start using it
if let UnwrappedString = Cat { // assigning a tempory variable that only exists for this if statment
// then you can you can use it however you like if it actually holds a value
print("the cats real name is \(UnwrappedString)")// case where it actually holds a value
} else {
print("guess the cats name was just nil") // case where it does not hold a value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.