-
-
Save ptraca/0456ae6faa15f211e45e8e25232a4ed4 to your computer and use it in GitHub Desktop.
Dynamically create instance based on type in Swift
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
protocol Initializable { | |
init() | |
} | |
class A : Initializable { | |
var content:String | |
required init() { | |
content = "TestContent" | |
} | |
} | |
func createInstance<T where T:Initializable>(typeThing:T.Type) -> T { | |
return typeThing() | |
} | |
let a = createInstance(A) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment