On Aug 15, 2008, at 3:28 PM, dct wrote:

I don't believe that my 'close' problem is explained by an awkward init -- as I said before, all of the other U/I operations of the nib "SPlotWindow.nib" are working as expected (so somehow I've dodged a potential bullet, at least so far).

Indeed; your application may be behaving the way you'd like it to for the time being, but it's not doing so for the reasons you expect. That's something you'll need to fix to keep your application maintainable.

I do wonder why it is improper to:
 1. load a nib, then
 2. init a WindowController instance using that nib, and then
3. init an NSView instance defined as part of the nib using the nib defined frame
all within a subclass init method.

It's improper because you're doing the same thing multiple times, and re-initializing objects that are already initialized. Here's what your original code looked like, with its lines numbered:

    1  - init
    2  {
    3      [NSBundle loadNibNamed:@"SPlotWindow.nib" owner:self];
    4      [super initWithWindowNibName:@"SPlotWindow.nib"];
    5      [splotView initWithFrame:[splotView frame]];
    6      return self;
    7  }

The problems with this are:

(1) You're not assigning to "self" when invoking your superclass designated initializer. That may return a different object; you must return the object you got back from your superclass designated initializer except in very special cases.

(2) On line 3, you're loading a nib file -- and setting its owner to a not-yet-initialized object! -- and then on line 4 you're basically telling your superclass to load that nib again. Line 3 is entirely redundant. Get rid of it.

(3) On line 5, you're not initializing "splotView" with information from the nib. You're **re-initializing** "splotView" with **its own** frame. This will have undefined results, because either your nib won't be loaded yet (once you get rid of line 3) or because initializing the same object twice could put it into a bad state. Furthermore, the frame of "splotView" will be set to whatever it is in the nib automatically, since you've said "splotView" comes from the nib.

In summary, your -init method should really look like Charles Steinman said it should:

    1  - (id)init
    2  {
    3      return [super initWithWindowNibName:@"SPlotWindow.nib"];
    4  }

There's no need to assign to "self" here because you're not doing anything with it, but if you were you should follow the standard Cocoa "self = ...; if (self != nil) { ... } return self;" pattern.

  -- Chris

_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to