Created
June 26, 2015 21:24
-
-
Save afarnham/1e80647c23eb36cf99f2 to your computer and use it in GitHub Desktop.
Dynamic instantiation of Swift classes conforming to a specific protocol
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 UIKit | |
protocol FooProtocol { | |
init() | |
func description() -> String | |
} | |
class Bar : FooProtocol { | |
required init() { | |
} | |
func description() -> String { | |
return "Instance of \(self)" | |
} | |
} | |
let dict: [String : FooProtocol.Type] = ["class" : Bar.self] | |
if let klass = dict["class"] { | |
klass.init().description() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment