I have the enviable situation of working for the guy who wrote +alloc
back in 1991, and I happened to ask him a very similar question a few months ago. The addition of +alloc
was in order to provide +allocWithZone:
, which was in order to add memory pools in NeXTSTEP 2.0 where memory was very tight (4M). This allowed the caller to control where objects were allocated in memory. It was a replacement for +new
and its kin, which was (and continues to be, though no one uses it) a 1-phase constructor, based on Smalltalk's new
. When Cocoa came over to Apple, the use of +alloc
was already entrenched, and there was no going back to +new
, even though actually picking your NSZone
is seldom of significant value.
So it isn't a big 1-phase/2-phase philosophical question. In practice, Cocoa has a single phase construction, because you always do (and always should) call these back-to-back in a single call without a test on the +alloc
. You can think of it as a elaborate way of typing "new".
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…