Is NSMutableArray *myArray = [[NSMutableArray alloc] init];
any more or less correct than NSMutableArray *myArray = [NSMutableArray new];
in terms of style or efficiency?
Asked
Active
Viewed 83 times
0

Apollo
- 8,874
- 32
- 104
- 192
-
The two are identical in function. `new` is probably underused, mainly out of habit (and a little bit of fear). – Hot Licks May 15 '14 at 22:49
2 Answers
0
Actually both are used to initialize the new object. new will also do allocate and initialize. But the apple recommends that call alloc first, then after invoke the init method. Thats why we are using [[NSMutableArray alloc] init]
Anyway, there is no big difference here

manujmv
- 6,450
- 1
- 22
- 35
-
1Even if you are using `new`. `alloc` will do first then only initialize it. In fact both are same – Anil Varghese May 15 '14 at 06:36
0
Neither is "more correct," and in fact they are functionally equivalent, but alloc
is much more common than new
since custom initialization is always done through initWith...
-style methods. So it's hard to say something is stylistically correct, but new
is certainly less stylistically standard in modern Objective-C.

Chuck
- 234,037
- 30
- 302
- 389
-
This should be the accepted answer. Modern objective C prefers `new` unless if you are not supposed to any custom initialisers like `initWith..` – Anil Varghese May 15 '14 at 06:41
-
Actually Apple said on last years WWDC or the year before that `new` is a deprecated style. New classes hardly ever have a `new` method, Apple highly discourages you to offer them in your own classes and they even gave a technical reason why one shouldn't use them, but I cannot remember what it was. – Mecki May 15 '14 at 22:21
-
@Mecki: It's definitely true that `new` is the old style. `new` is how it used to be done back when Objective-C leaned more in the Smalltalk direction. But it's not *quite* true that "new classes hardly ever have a `new` method". They inherit NSObject's `new` just like all other classes do, which is equivalent to alloc+init. If you can write `[[SomeNSObjectSubclass alloc] init]`, you can write `[SomeNSObjectSubclass new]`. It's not marked as deprecated; it's just stylistically nonstandard. – Chuck May 15 '14 at 22:59