Sylvain <sam.g@[EMAIL PROTECTED]
> writes:
> Hello,
>
> I have an object B which fills a buffer (NSData *) buf
> asynchronously. From my class A, I want to instantiate B but A needs
> to know when the buf buffer is ready.
>
> I would like to know which mechanism I should use:
> * Should I pass a pointer to the calling class to B and in B send a
> message to A, like:
> (in A:)
> B *b = [[B alloc] initWithCaller: a];
> - (void) dataReady: (NSData *) buf {doSomethingWith: buf;}
> (in B:)
> [a dataReady: buf]
>
> * Should I use selectors? (how do I do that)
The best way to answer that question is with another question: Do you want
to provide the author of A with the ability to use a different name for
the
callback method? Or are you just as happy saying "this is the name of the
callback method you must implement"?
Plenty of examples both ways - I don't think there's a consensus about
which
way is universally "right."
You can use selectors to call methods with NSObject's -performSelector:
and
its similarly-named relatives. Those methods only sup****t object (id) args
and return values - if you need more flexibility, you should have a look
at
the NSInvocation class.
If the callback method is optional, you can use NSObject's
-respondsToSelector:
to check before calling it.
Callback and delegate methods often take the sender (in this case, an
instance
of B) as an argument. So the method could be something like
-theB:hasData:,
which would allow an instance of A to gather and handle data from many B
instances.
If you do allow A users to supply a selector, you should use NSObject's
-methodSignatureForSelector: to get an NSMethodSignature instance that
describes it. You can use that to verify that it has the correct return
type, and the correct number and type of any arguments.
sherm--
--
My blog: http://shermspace.blogspot.com
Cocoa programming in Perl: http://camelbones.sourceforge.net


|