Reputation: 8336
Will there be any performance benefits in using NSClassFromString? Especially when comparing with the standard way of importing the class from header and initializing.
Upvotes: 2
Views: 537
Reputation: 3456
NSClassFromString is a runtime commande, which mean it will perform, at runtime, a fetch in a "database" to find a loaded class matching the given name. Firstly, it is not recommended to use it when you could use the basis #import pattern (which will perform the fetch at compile time, so the final user won't notice it). it will provide less code safety, which could lead to "really hard to debug" issues. It might comes a time when you need it, for example when creating a Class at runtime, or when trying to parse a json object or dictionaries to concrete class.
Upvotes: 0
Reputation: 122391
I don't see how you can think that using NSStringFromClass()
is an alternative to importing the class declaration and letting the compiler do the work.
Given the compiler won't have seen the declaration, the object will be next to useless and you would be forced to convert strings into selectors in order to be able to call them.
It certainly won't be faster than importing the declaration via the header file.
Upvotes: 0
Reputation: 6517
Not at all. If anything, NSClassFromString
is slower than the normal way of using a class!
Upvotes: 1