Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift init from unknown class
import Cocoa
protocol Thing {
var x: String {get}
init(s: String)
}
class Foo: Thing {
let x: String
required init(s: String) {
x = s
}
}
class Bar: Thing {
let x: String
required init(s: String) {
x = s
}
}
func classToUse() -> Thing.Type {
return Foo.self //maybe it's Bar sometimes.
}
let someClass = classToUse()
let something = someClass.init(s:"A string")
something.x
// Works now! The answer was the init call.
// See David Owens II’s gist: https://gist.github.com/owensd/bfe4ff8e17bbf6db842c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment