[ad_1]
Asked
Viewed
107k times
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?
4
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 (likeinitWithString
)alloc-init
is more explicit thannew
General opinion seems to be that you should use whatever you’re comfortable with.
12
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...
5
+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
.
6
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]
.
One Short Answere is:
- Both are same. But
- ‘new’ only works with the basic ‘init’ initializer, and will not
work with other initializers (eg initWithString:).
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.
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.
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]
.
1
lang-objectivec
[ad_2]