ftp.nice.ch/peanuts/GeneralData/Usenet/news/1991/CSNProg-91.tar.gz#/comp-sys-next-programmer/1991/Aug/Question-about-self-and-delegates

This is Question-about-self-and-delegates in view mode; [Up]


Date: Sun 30-Aug-1991 09:01:34 From: carlton@cs.berkeley.edu (Mike Carlton) Subject: Question about self and delegates I've got a question for the obj-c gurus: I'm playing around with a simple text editor, trying to get my feet wet. I've got a Document class and I define a delegate method window. So it looks something like this: @interface Document:Object { id window; id scrollview; id text; const char *name; int titled; } .. @end @implementation Document - init { ... [window setDelegate:self]; } - windowWillClose:sender { if ([window isDocEdited]) { ... } return self; } @end I would expect that within the windowWillClose: method self would be a Document (after all, it is defined as a Document method). _But_, at runtime, self within windowWillClose: is a FrameView ?! (i.e. p self->isa->name returns "FrameView"). Now sender (which is a Window) has the correct delegate -- p sender->delegate->isa->name returns "Document". Why isn't self a Document? gdb thinks it should be a Document: if you enter p self it prints '(struct Document *) 0x72f58'. If you print *self, you get a bunch of bogus values (because *self is really a FrameView). Can anyone tell me what is going on? What am I missing? thanks in advance, mike (carlton@cs.berkeley.edu)
Date: Sun 04-Sep-1991 03:18:35 From: wiml@milton.u.washington.edu (William Lewis) Subject: Re: Question about self and delegates In article <1991Aug30.090134.2292@agate.berkeley.edu> carlton@cs.berkeley.edu writes: >I'm playing around with a simple text editor, trying to get my feet >wet. I've got a Document class and I define a delegate method >windowWillClose: and set the document to be the delegate for the >window. So it looks something like this: ... >@implementation Document > >- init >{ > ... > [window setDelegate:self]; >} This could be the problem. The usual method of generating a new object in the absence of the +new method is anObj = [[Something alloc] init]; Notice that your -init method isn't explicitly returning anything. The caller will get a garbage value, which I suppose happens to be a FrameView. Because of the way function returns are implemented by the compiler, this is probably the old delegate, unless I'm forgetting something (entirely possible). Thus the window's delegate variable would be set to the 'real' self (since you just passed it) but immediately afterwards the self would be lost.

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Marcel Waldvogel and Netfuture.ch.