Re: Is "-init" really needed?
site_archiver@lists.apple.com Delivered-To: cocoa-dev@lists.apple.com
On Aug 7, 2017, at 5:02 PM, David Hoerl <dhoerl@mac.com> wrote:
I recently saw some code where an object was alloced but there was no init:
Foo *foo = [Foo alloc]; foo.bar = ...
My blood pressure soared! My pulse quickened! I started breathing rapidly!
But then I though - heck, if Foo has NSObject as its super class, gee, maybe -init isn't really need. I mean, if all of Foo's ivars and properties are initialized, its a shortcut, right.
Pro: * -[NSObject init] is in fact guaranteed to do nothing. Con: * As you noticed, independent of its correctness, the code above *looks* wrong. That alone is bad. It ought either to call -init or to have a big scary comment describing why it is safe not to. * If Foo is ever changed to have a superclass other than NSObject, the code above is likely to be wrong. -- Greg Parker gparker@apple.com <mailto:gparker@apple.com> Runtime Wrangler _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/site_archiver%40lists.appl... This email sent to site_archiver@lists.apple.com
participants (1)
-
Greg Parker