On May 29, 2015, at 1:40 PM, Jean-Daniel Dupas wrote: > >> Le 29 mai 2015 à 19:22, Alex Zavatone <z...@mac.com> a écrit : >> >> Was just looking at good old object initialization and came across a stupid >> idea. >> >> For most object initialization, we do this: >> >> - (id)init { >> if (self = [super init]) { >> // Set up stuff here. >> // this could get long. >> } >> return self; >> } >> >> in some cases, the set up within the parens could get pretty long. In cases >> like that, I generally set up another method to handle that for >> organization, but if you're passing objects into into your init method, then >> you're passing more data again and the code could get less cleaner looking >> than it could be. >> >> So, I thought, "why don't we check if self != [super init] and then >> immediately return if that is the case?" >> >> That would change object initialization to this: >> >> - (id)init { >> if (self != [super init]) { >> return; >> } >> >> // Set up stuff here. >> // We don't care if this gets long. >> // We really don't. This could go on and on. >> >> return self; >> } >> > > And now that a reread the code, it is patently wrong. > > if (self = [super init]) is a assignment to self not a comparison. > > If you want to do a fast path, you have to do > > self = [super init]; > if (!self) > return nil; >
Thanks much. I knew there was something I was missing there that one of you would see. Would this handle it properly? if (!(self = [super init])) { return nil; } _______________________________________________ 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/archive%40mail-archive.com This email sent to arch...@mail-archive.com