Last active
August 29, 2015 14:06
-
-
Save jonfriskics/a95d3701c71793872de5 to your computer and use it in GitHub Desktop.
Function Argument Unwrap Optional
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
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