This is How-does-an-object-have-a-delegate? in view mode; [Up]
Date: Sun 25-Nov-1991 15:50:19 From: fasano@unix.cis.pitt.edu (Cathy Fasano) Subject: How does an object have a delegate? I can find plenty of documentation on how to *be* a delegate, but no documentation on how to *have* a delegate. Does the following sound reasonable? The object that I want to send the delegate message is an object that represents a stock, and this object is receiving real-time hits from a ticker feed every time the stock price changes. What I would like to have the stock object do is send a mktValueDidChange: message to its delegate every time it gets a ticker-feed hit. Here's what I did: added an outlet called delegate to the stock object initialized delegate to nil in the object's init method added a method setDelegate: which does the following: - setDelegate:dlgte { delegate = dlgte; return self; } I implemented the following macro: #define privateSetMktValue(aDouble) \ do if(fabs(mktValue - aDouble) > .000001) \ { mktValue = aDouble ; \ if (delegate != nil && \ [delegate respondsTo:@selector(mktValueDidChange:)]) \ [delegate mktValueDidChange:self]; \ } while(0) Then, each time I get a realtime hit, I call privateSetMktValue() on the number I receive. (I used a macro so that I don't get messaging overhead on every realtime hit when the delegate is nil.) Is this the right way to do this? Is there a good way to get around sending the respondsTo: message to the delegate every time? The compiler complains because mktValueDidChange: is not declared anywhere. Since it "does the right thing" (implicitly declares it to be type id), should I just ignore the warning, or is there a way to fix it? My apologies in advance if the answer to this is rtfm -- I looked for it in the documentation, honest! cathy :-)-- Cathy Fasano aka: Cathy Johnston, fasano@unix.cis.pitt.edu cathy@gargoyle.uchicago.bitnet, uunet!unix.cis.pitt.edu!fasano "If there's no solution, then it isn't a problem." -- Evening Shade
Date: Sun 25-Nov-1991 22:00:20 From: mcgredo@bluto.ie.orst.edu (Don McGregor) Subject: Re: How does an object have a delegate? Cathy Fasano writes > >Is there a good way to get around >sending the respondsTo: message to the delegate every time? Make sure all your delegates respond to the message, then remove the well-defined objects to act as delegates. Your code will be less robust, but you will get a slight speed increase. It might be significant if your're really banging on the object with delegate messages.
Date: Sun 25-Nov-1991 22:33:45 From: ksbrain@zeus.UWaterloo.ca (Kevin Brain) Subject: Re: How does an object have a delegate? In article <199308@unix.cis.pitt.edu> fasano@unix.cis.pitt.edu (Cathy Fasano) > Is there a good way to get around sending the respondsTo: message > to the delegate every time? If you're absolutely sure the object you are sending it to does respond, you could just go ahead and send it. However, your program will crash (with a 'Object doesn't respond to method' message to the console) if the object doesn't. > The compiler complains because mktValueDidChange: is not declared > anywhere. A good way to declare delegate methods is using a category in the object's .h file. This is how NeXT does it (see, for example, Window.h). @interface Object(StockObjectDelegate) - mktValueDidChange: @end Good luck! Kevin
Date: Sun 26-Nov-1991 03:43:50 From: apf@unl.fct.unl.pt (Antonio Pascoa) Subject: Re: How does an object have a delegate? In article <199308@unix.cis.pitt.edu> fasano@unix.cis.pitt.edu (Cathy Fasano) > ... > Is this the right way to do this? Is there a good way to get around > sending the respondsTo: message to the delegate every time? The compiler > ... - setDelegate:dlgte { _flags.sendMktValueDidChange = delegate != nil && \ [delegate respondsTo:@selector(mktValueDidChange:)]; delegate = dlgte; return self; } #define privateSetMktValue(aDouble) \ do if(fabs(mktValue - aDouble) > .000001) \ { mktValue = aDouble ; \ if (_flags.sendMktValueDidChange) \ [delegate mktValueDidChange:self]; \ } while(0) Notice that even if you set the delegate using Interface Builder, setDelegate: will be called on .nib loading. Boas, Pascoa
Date: Sun 27-Nov-1991 13:58:46 From: hwr@pilhuhn.ka.sub.org (Heiko W.Rupp) Subject: Re: How does an object have a delegate? In article <199308@unix.cis.pitt.edu>, Cathy Fasano writes: > I can find plenty of documentation on how to *be* a delegate, but no > documentation on how to *have* a delegate. Does the following sound > reasonable? If your delegate is an instantiated class object, you may control-drag a connection from your sender to that object. [sorry if that's wrong - I don't have a NeXT handy to check it out]
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Marcel Waldvogel and Netfuture.ch.