Created
June 21, 2014 00:30
-
-
Save rodericj/e84e827be93d43ef7b40 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Cocoa | |
class StringHolder { | |
var a : String? | |
var b : String? | |
var c : String? | |
func naiveDescription() -> String { | |
return a! + b! + c! | |
} | |
func preferredDescription() -> String { | |
var builtString : String = "" | |
if let defA = a? { | |
builtString += defA | |
} | |
if let defB = b? { | |
builtString += defB | |
} | |
if let defC = c? { | |
builtString += defC | |
} | |
return builtString | |
} | |
} | |
var holder = StringHolder() | |
holder.a = "a String " | |
holder.b = "b string" | |
println(holder.preferredDescription()) // successfully prints the description | |
println(holder.naiveDescription()) // since c is not set and it is forced, this crashes |
@joshaber yes that is concise. I'll take it. Thank you for that.
Awww, @joshaber looks like they broke this in the latest. wah waaaahhh. I'll play around
Looks like it may just be the implicit comparison to nil. Got it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Something like this would be a bit more general: