Skip to content

Instantly share code, notes, and snippets.

@jonfriskics
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonfriskics/a95d3701c71793872de5 to your computer and use it in GitHub Desktop.
Save jonfriskics/a95d3701c71793872de5 to your computer and use it in GitHub Desktop.
Function Argument Unwrap Optional
func myFunc1(name: String!) {
println(name); // playground shows "Jon1" is printed
}
func myFunc2(name: String) {
println(name); // playground shows "Jon2" is printed
}
let str1:String? = "Jon1"; // playground shows str1 is {Some "Jon1"}
let str2:String = "Jon2"; // playground shows str2 is "Jon2"
myFunc1(str1);
myFunc2(str2);
/* if you do this, though, you'll get an error because you're passing in an optional that's never being unwrapped */
func myFunc3(name: String) {
println(name);
}
let str3:String? = "Jon3";
myFunc2(str3); // error: Value of optional type String? not unwrapped; did you mean to use '!' or '?'?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment