Use of alloc init instead of new

Objective COop

Objective C Problem Overview


Learning Objective-C and reading sample code, I notice that objects are usually created using this method:

SomeObject *myObject = [[SomeObject alloc] init];

instead of:

SomeObject *myObject = [SomeObject new];

Is there a reason for this, as I have read that they are equivalent?

Objective C Solutions


Solution 1 - Objective C

There are a bunch of reasons here: http://macresearch.org/difference-between-alloc-init-and-new

Some selected ones are:

  • new doesn't support custom initializers (like initWithString)
  • alloc-init is more explicit than new

General opinion seems to be that you should use whatever you're comfortable with.

Solution 2 - Objective C

Very old question, but I've written some example just for fun — maybe you'll find it useful ;)

#import "InitAllocNewTest.h"

@implementation InitAllocNewTest

+(id)alloc{
    NSLog(@"Allocating...");
    return [super alloc];
}

-(id)init{
    NSLog(@"Initializing...");
    return [super init];
}

@end

In main function both statements:

[[InitAllocNewTest alloc] init];

and

[InitAllocNewTest new];

result in the same output:

> 2013-03-06 16:45:44.125 XMLTest[18370:207] Allocating... > 2013-03-06 16:45:44.128 XMLTest[18370:207] Initializing...

Solution 3 - Objective C

+new is equivalent to +alloc/-init in Apple's NSObject implementation. It is highly unlikely that this will ever change, but depending on your paranoia level, Apple's documentation for +new appears to allow for a change of implementation (and breaking the equivalency) in the future. For this reason, because "explicit is better than implicit" and for historical continuity, the Objective-C community generally avoids +new. You can, however, usually spot the recent Java comers to Objective-C by their dogged use of +new.

Solution 4 - Objective C

Frequently, you are going to need to pass arguments to init and so you will be using a different method, such as [[SomeObject alloc] initWithString: @"Foo"]. If you're used to writing this, you get in the habit of doing it this way and so [[SomeObject alloc] init] may come more naturally that [SomeObject new].

Solution 5 - Objective C

One Short Answere is:

  1. Both are same. But
  2. 'new' only works with the basic 'init' initializer, and will not work with other initializers (eg initWithString:).

Solution 6 - Objective C

I am very late to this but I want to mention that that new is actually unsafe in the Obj-C with Swift world. Swift will only create a default init method if you do not create any other initializer. Calling new on a swift class with a custom initializer will cause a crash. If you use alloc/init then the compiler will properly complain that init does not exist.

Solution 7 - Objective C

For a side note, I personally use [Foo new] if I want something in init to be done without using it's return value anywhere. If you do not use the return of [[Foo alloc] init] anywhere then you will get a warning. More or less, I use [Foo new] for eye candy.

Solution 8 - Objective C

If new does the job for you, then it will make your code modestly smaller as well. If you would otherwise call [[SomeClass alloc] init] in many different places in your code, you will create a Hot Spot in new's implementation - that is, in the objc runtime - that will reduce the number of your cache misses.

In my understanding, if you need to use a custom initializer use [[SomeClass alloc] initCustom].

If you don't, use [SomeClass new].

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwillc2View Question on Stackoverflow
Solution 1 - Objective CJeremy StanleyView Answer on Stackoverflow
Solution 2 - Objective CradekEmView Answer on Stackoverflow
Solution 3 - Objective CBarry WarkView Answer on Stackoverflow
Solution 4 - Objective CBrian CampbellView Answer on Stackoverflow
Solution 5 - Objective Cuser739711View Answer on Stackoverflow
Solution 6 - Objective CMurderDevView Answer on Stackoverflow
Solution 7 - Objective Cevdude100View Answer on Stackoverflow
Solution 8 - Objective CMike CrawfordView Answer on Stackoverflow