I want to instantiate a class by using a string but I'm having problems. That's what I try to do:
let aClass = NSClassFromString("MyApp.Outer.Inner") as! SomeProtocol.Type
....
public class Outer {
public class Inner: SomeProtocol {
init() {...}
}
}
That's crashing (because NSClassFromString returns nil
But that's working:
let aClass = NSClassFromString("MyApp.Inner") as! SomeProtocol.Type
....
public class Inner: SomeProtocol {
init() {...}
}
Doesn't NSClassFromString
work for nested classes?
Background:
I have many "inner" classes that I use as templates to create objects (instead of storing them in .plist
files, which is kind of annoying). I want to encapsulate them in an "Outer" class to have them better organised.
Thanks for any help :)
EDIT: I saw the question that this seems to be a duplicate of. But that's not a viable solution:
(Note this snippet won't run by itself -- the class A.B has to be referenced at least once somewhere in your process in order to be registered with the ObjC runtime. This could be as simple as putting let t = A.B.self somewhere in your app's startup code.)
I'm doing this approach with NSClassFromString
because I want to avoid adding too much stuff for every inner class I create. I don't want to constantly add this to my startup code.