object-c study notes: new vs alloc init

Original link: http://www.cnblogs.com/yang3wei/archive/2012/09/27/2739350.html

Reprinted from: http://www.cnblogs.com/ulihj/archive/2011/01/15/1936342.html

In object-c foundation tutorial in this book can always see something like this kind of code, my little rookie wonder, what difference ah, what look the same. Internet looking to find, find some argument, here to pick up.
    * Object = someClass [someClass new new];
or
    someClass * Object = [[someClass alloc] init];

in fact, is the same, a certain man had this to say, new in alloc and init called internally,
Actually "new new" IS not a keyword in Objective-C, but NSObject implements a class method "new" which simply calls "alloc" and "init".

Since, like why there are two, eat that Han was it. .
The new method actually comes from NeXT days . Back then, there was no two phase initialization, it was just a single new method. They soon realized that a two phase approach could have advantages, and introduced alloc. New was kinda deprecated, but kept in for backwards compatibility. It is exactly the same as alloc-init. Use 'new' if it suits. One shortcoming is that it only works with the basic 'init' initializer, and will not work with other initializers (eg initWithString :) .
background note, new wording is a more old-fashioned, and only later found a new so bad, was introduced alloc and init such an approach, to retain new one is backward-compatible, and second, many times is a simpler wording. As alloc such an approach can conjure up such flowers,

Frequently, you are going to need to pass arguments to init and so you will be using a different method, such as [[SomeObject alloc] initWithString: @ "Foo"]. If you're used to writing this, you get in the habit of doing it this way and so [[SomeObject alloc] init] may come more naturally that [SomeObject new].
Well, actually if you do not use other init function, such as initWithString, with the new method, there is no doubt more convenient.

Again a source said to be more reliable to say:
There was a very long thread on this same subject (alloc / init vs. new) on the cocoa-dev mailing list this week (search for "[Foo new] vs [[Foo alloc] init]"). Unfortunately the documentation is not crystal clear on this, but Bill Bumgarner (an Apple Engineer) confirmed that new is implemented as allocWithZone / alloc followed by init back at least to the beginning of OS X. So the answer is, use whichever you prefer. The current vogue in Cocoa programming is to use alloc / init because it makes the intended behavior explicit.
Well, it seems that only show the call more easily understand this difference, in a word, ¥ ck.

The brothers come to a concluding remarks, tentatively we first believed in him,
    * new new does not Support Custom initializers (like initWithString)
    * alloc-the init IS More Explicit Within last new new
General Opinion Seems to BE that you Should Whatever you use ' re comfortable with.

Above stories are from http://macresearch.org/difference-between-alloc-init-and-new, are interested to see to.

Reproduced in: https: //www.cnblogs.com/yang3wei/archive/2012/09/27/2739350.html

Guess you like

Origin blog.csdn.net/weixin_30455067/article/details/94783108