If I have an
NSObject
subclass which either has no-init
method or simply does nothing in-init
, is there any difference between an instance created these two ways:
MyClass *instance = [MyClass alloc];
MyClass *instance = [[MyClass alloc] init];
Technically, there is no difference.
But that doesn't mean you should use a bare +alloc
to ever create an instance for a variety of reasons.
First, it is the principal of the thing. Objective-C coding standards say +alloc
should always be followed by -init
.
Secondly, it is all about consistency and code maintenance. What happens when you refactor MyClass
to be a subclass of some class where the designated initializer is actually critical? A nasty, hard to figure out, bug is what happens.
Of relevance, note that the use of +new
has been all but deprecated for a similar reason. It makes refactoring tedious (dammit! gotta break apart THIS call site, too!) and the convenience factor is exceedingly minimal.