Skip to content

Instantly share code, notes, and snippets.

@drosenstark
Created May 27, 2015 17:20
Show Gist options
  • Save drosenstark/60e54c1aa38234fa6133 to your computer and use it in GitHub Desktop.
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.
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