0

Possible Duplicate:
alloc, init, and new in Objective-C

There is +alloc/-init... and +new. Their role are almost same in documentation except +new is designed for instance pooling. (according to my understanding :)

However the pooling is possible with +alloc/-init. Why the separated method is required? Or is there any reason for the method?

And I couldn't find any example utilizes the method. When should I use this method? Can I get some use-case of the method +new?

Community
  • 1
  • 1
eonil
  • 83,476
  • 81
  • 317
  • 516

1 Answers1

1

In the original Objective-C implementation from Stepstone, +new was what we used to create instances. It was a holdover from Smalltalk. NeXT separated +new into +alloc/-init since most classes don't have to do anything different to allocate the memory their instances use, and it didn't make sense to duplicate that code all over the place.

NSResponder
  • 16,861
  • 7
  • 32
  • 46
  • 3
    `+[NSOject new]` is implemented as `return [[self alloc] init]`. Bear that in mind if using `new`, since this means that the designated initializer is not guaranteed to be called. In short; avoid `new`. – PeyloW Aug 03 '11 at 08:40