Last active
October 24, 2017 05:55
-
-
Save dominickm/cd5c2cba27bc96be6d7f556f824499ed to your computer and use it in GitHub Desktop.
Coder Radio 280: 1
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
// for Coder Radio 280 | |
//: Playground - noun: a place where people can play | |
import UIKit | |
class Gungan { | |
var name: String = "" // I am not optional, so I must have an initializer | |
var fifthGradeDiploma: String? // I, sadly, am an optional | |
func greet(jedi: String) -> String { | |
return "Hey " + jedi + " messa " + name + "!"; | |
} | |
func darkSideGreet(darkJedi: String!) -> String { | |
return "Hey " + darkJedi + " messa " + name + "!"; | |
} | |
func bragAboutEducation() -> String { | |
if let education = fifthGradeDiploma { | |
return "Messa went to " + education + " up to grade five!"; | |
} else { | |
return "Messa no know what school is..."; | |
} | |
} | |
} | |
let jar = Gungan(); | |
// if you pass a valid value here you are good to go | |
jar.greet(jedi: "Qui Gon"); | |
// because 'jedi' is not marked as optional, the Swift compiler will throw a dev time error | |
jar.greet(jedi: nil); | |
// this is going to work fine with a good a value | |
jar.darkSideGreet(darkJedi: "Count Dooku"); | |
// however, if you pass nil, then run time pain... | |
//fatal error: unexpectedly found nil while unwrapping an Optional value | |
jar.darkSideGreet(darkJedi: nil); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment