As cobbal [[cls alloc] init] noted, this is common practice. alloc is a static class method defined in NSObject that allocates memory, and init is an instance constructor. Many classes provide convenience constructors that do this in one step for you. Example:
NSString* str = [NSString stringWithString:@"Blah..."];
Note * after NSString . You mainly work with C here, so pointers to objects!
Also, do not forget to free the allocated memory using alloc with the corresponding [instance release] . You do not need to free up memory created using the convenience constructor, as this is auto-implemented for you. When you return your new cls instance, you should add it to the autodetection pool so that there is no memory leak:
return [[[cls alloc] init] autorelease];
Hope this helps.
Greg sexton
source share