Created
May 27, 2015 17:20
-
-
Save drosenstark/60e54c1aa38234fa6133 to your computer and use it in GitHub Desktop.
Sometimes you need one of these, one of those, or nothing... this is an example of that case.
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
enum ObjectOrString { | |
case Object(NSObject) | |
case Text(String) | |
case Empty | |
func object() -> NSObject? { | |
switch self { | |
case let .Object(obj): return obj | |
default: return nil | |
} | |
} | |
func text() -> String? { | |
switch self { | |
case let .Text(text): return text | |
default: return nil | |
} | |
} | |
} | |
let one = ObjectOrString.Object(NSObject()) | |
let two = ObjectOrString.Text("yeah there") | |
let mixedArray : [ObjectOrString] = [one, two, ObjectOrString.Empty] | |
for thing in mixedArray { | |
if let obj = thing.object() { | |
println("Yes it's an object \(obj.hash)") | |
} else if let text = thing.text() { | |
println("Yes it's a string \(text)") | |
} else { | |
println("it's blank") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment