Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Objective-c > Re: Callback me...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 4 Topic 957 of 1039
Post > Topic >>

Re: Callback methods

by Sherman Pendley <spamtrap@[EMAIL PROTECTED] > Feb 25, 2008 at 04:56 PM

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
 




 4 Posts in Topic:
Callback methods
Sylvain <sam.g@[EMAIL   2008-02-25 11:49:07 
Re: Callback methods
Sherman Pendley <spamt  2008-02-25 16:56:10 
Re: Callback methods
Sylvain <sam.g@[EMAIL   2008-02-25 23:12:53 
Re: Callback methods
costello@[EMAIL PROTECTED  2008-02-26 12:46:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 7 7:56:38 CDT 2008.