Skip to content

Instantly share code, notes, and snippets.

@dcunited001
Created March 11, 2016 05:47
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 dcunited001/ecddc33e644ec379f22a to your computer and use it in GitHub Desktop.
Save dcunited001/ecddc33e644ec379f22a to your computer and use it in GitHub Desktop.
// i have a protocol
protocol Proto {
var aVar: String
typealias Alias // and it has a type alias
}
enum TypesOfProtos {
case FooTag = "foo-tag"
case BarTag = "bar-tag"
// can't use this one because Proto uses type aliases
func getProto() -> Proto {
switch self {
case FooTag: return FooTag()
case BarTag: return BarTag()
default: return nil
}
}
// can't use this one because i have to know the type ahead of time...
func getProtoGeneric<T: Proto>() -> T? {
switch self {
case FooTag: return FooTag()
case BarTag: return BarTag()
default: return nil
}
}
// can't use this because i can't seem to instantiate with
// the class returned from the result of this method
func getKlass() -> AnyClass {
switch self {
case FooTag: return FooTag.self
case BarTag: return BarTag.self
// default: return nil // i also tried returning optional
}
}
}
class FooTag {
var aVar: String = "fdsa"
}
class BarTag {
var aVar: String = "asdf"
}
let typeProtoFoo = TypesOfProtos(rawValue: "foo-tag")
let typeProtoBar = TypesOfProtos(rawValue: "bar-tag")
// again, i can't use this one
let fooTag = typeProtoFoo.getProto()
// and again, i can't use this one because the whole point is that
// I want the method to handle figuring out what type it is from the enum values
let barTag = typeProtoBar.getProtoGeneric()
// and i also can't use this one:
let barTag = typeProtoBar.getKlass().self.init() // wrong
let barKlass = typeProtoBar.getKlass()
let barTag = barKlass.init() // nope
let barTag = barKlass.dynamicType.init() // wrong
let barTag = barKlass.self.init() // nope
let barTag = barKlass() // nope
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment