Last active
August 29, 2015 14:25
-
-
Save brentsimmons/2080595ac8a6f41711af to your computer and use it in GitHub Desktop.
Swift init from unknown class
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
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