Vivek Mohan
Vivek Mohan

Reputation: 8336

NSClassFromString performance

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

Answers (3)

Florian Burel
Florian Burel

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

trojanfoe
trojanfoe

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

Michael
Michael

Reputation: 6517

Not at all. If anything, NSClassFromString is slower than the normal way of using a class!

Upvotes: 1

Related Questions