Suppose I have three classes:
import Foundation
class A {
init() {
print("A")
}
}
class B {
init() {
print("B")
}
}
class C {
init() {
print("C")
}
}
I want to dynamically pass a string ("A", "B" or "C") as an argument to a function, and then create an instance of the class that I passed inside the body of this function. Is it possible? How?
I tried this (and other options) but no luck:
func test(c:AnyObject){
let _class = c()
//...
}
test(c:A)
[UPDATE] This question may be no different from the one suggested by @Code Different, but this question is old and there have been so many changes in the language that you need to try any proposed solution before you find the one that works for today day
user1094081
source
share