Skip to content

Instantly share code, notes, and snippets.

@gabericharde
Last active June 16, 2016 18:57
Show Gist options
  • Save gabericharde/12561bb9df15fd181dbf1097fc5eff07 to your computer and use it in GitHub Desktop.
Save gabericharde/12561bb9df15fd181dbf1097fc5eff07 to your computer and use it in GitHub Desktop.
// Why optionals as a type in Swift? Because Swift dissalows nil values in all other types!
// So the question is, under what circumstances does a value need to be nil?
//: 1. A method that cannot return a value
var y: Int
var s1: String
var s2: String
s1 = "123"
s2 = "ABC"
y = Int(s1) //this shit should throw warnings in xcode
y = Int(s2) //this too
//: 2. Properties that cannot be initialized when an object is constructed
class ViewController: UIViewController {
//var button: UIButton // button is a regular Swift variable, so cannot store a nil value
}
//Use a ? after type declaration to declare var or let as optionals
// Exclamation mark implicitly unwraps optionals, basically throwing a fatal error if the object ever has a nil value
//So why use such a volatile optional?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment