问题
Never initialize an object without reassigning any pointer to that object. As an example, don’t do this:
NSObject *someObject = [NSObject alloc];
[someObject init];
If the call to init returns some other object, you’ll be left with a pointer to the object that was originally allocated but never initialized.
Actually, this is a example in Apple's ObjC document, but i'm not quite clear with this, that is, why NSObject *someObject = [[NSObject alloc] init]
can promise return the object we just needed, while NSObject *someObject = [NSObject alloc]; [someObject init];
can not?
回答1:
Just because -init could return something different from someObject. In your example you have to re-assign the pointer to the result of the -init.
来源:https://stackoverflow.com/questions/13301365/objc-why-it-is-incorrect-when-implement-alloc-and-init-methods-separatly