Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of alloc init instead of new

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?

like image 375
willc2 Avatar asked Apr 05 '09 23:04

willc2


1 Answers

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.

like image 166
Jeremy Stanley Avatar answered Oct 15 '22 09:10

Jeremy Stanley