Skip to content

Instantly share code, notes, and snippets.

@brentsimmons
Last active August 29, 2015 14:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentsimmons/2080595ac8a6f41711af to your computer and use it in GitHub Desktop.
Save brentsimmons/2080595ac8a6f41711af to your computer and use it in GitHub Desktop.
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