I wanted to instantiate a class and call a method of it dynamically.
class Test: NSObject {
@objc func greet() {
print("123")
}
}
I am able to call the method given in string form using perform
let t = Test()
t.perform(Selector("greet"))
But I also want to init the test in a similar way.
let t = GetClass("Test").init() // some method when called gives it's object
t.perform(Selector("greet"))
Here the Swift class will inherit from NSObject and have @obj
added to its methods.
How to do the same using Objective-C methods but from Swift source code? For example,
let t = NSClassFromString("Test")
t.init() // ??
t.perform(Selector("greet"))
Edit: For the Objective-C, I got it working by following Call a method from a String in Swift. Need to include module name to get this working.