Use of alloc init instead of new

willc2 picture willc2 · Apr 6, 2009 · Viewed 102.1k times · Source

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?

Answer

Jeremy Stanley picture Jeremy Stanley · Apr 6, 2009

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.