Let's suppose I've a class
class Test {
}
Then in another part of the program I could do
let testClass = NSClassFromString( "Test" ) as NSObject.Type
let testInstance = testClass()
Is there a way to do the same thing from reference of Test itself, pass Test as a reference and then create an object from the reference
By reference I mean pass the Test
itself and receiving part of the code would be able to create object out of it.
just like we used to do in Objective-C
TestClassRef = [Test class]
[[TestClassRef alloc] init]
Edit:
protocol ISomeProtocol {
func action ()
func init()
}
class Test
var classesMap = Dictionary<String,ISomeProtocol>()
func init () {
self.classesMap = Dictionary<String,ISomeProtocol>()
}
func myAction (name:String ) {
var myClass:ISomeProtocol;
if let myClass = self.classesMap[ name ] {
var myInstance = myClass() as ISomeProtocol
myInstance.action()
} else {
return
}
}
}