Skip to content

Instantly share code, notes, and snippets.

@dominickm
Last active October 24, 2017 05:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominickm/cd5c2cba27bc96be6d7f556f824499ed to your computer and use it in GitHub Desktop.
Save dominickm/cd5c2cba27bc96be6d7f556f824499ed to your computer and use it in GitHub Desktop.
Coder Radio 280: 1
// 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