Hi,
Wondering, what is the difference between creating a class with:
Class clazz = [NSString class];
[clazz alloc];
and
class_createInstance(clazz,0);
Hi,
Wondering, what is the difference between creating a class with:
Class clazz = [NSString class];
[clazz alloc];
and
class_createInstance(clazz,0);
One is a function, the other is a method. The function, by virtue of being a function, cannot be overloaded. The method, (since it's a method) could conceivably be implemented in a different manner.
For example, since some classes in Cocoa (collections, for example) are class clusters, it's possible that they override +alloc
to implement custom behavior. You could not do that with when using a function.
There is a description here:
http://www.cocoabuilder.com/archive/cocoa/111527-class-createinstance-with-nsarray-nsdictionary.html
Basically, class_createInstance is for Cocoa implementors, and gives them low-level access to the process. API users should use alloc, which presumably uses class_createInstance or something like it.
Basically, you shouldn't be using class_createInstance()
unless you know enough about what you're doing that you can answer this question yourself.
Calling class_createInstance()
bypasses any special cases that have been implemented in +alloc
. If you try it with NSString
, you will get an NSString
instance, not an instance of the private placeholder class that is the proper target for whatever -init...
message you want to send it.