Re: RunLoop and callback function
Re: RunLoop and callback function
- Subject: Re: RunLoop and callback function
- From: Chris Hanson <email@hidden>
- Date: Tue, 27 Jan 2004 15:50:56 -0600
On Jan 27, 2004, at 1:04 PM, Thierry Bucco wrote:
void PowerSourcesHaveChanged()
{
NSLog(@"ca a changi...");
[self doThisPlease]; // <- self undeclared
}
This is because PowerSourcesHaveChanged is a function, not a method.
Since it's just a function, it doesn't "belong" to a class and
therefore has no self, super, etc.
Also, you have an incorrect prototype for your PowerSourcesHaveChanged
method. It should be
void PowerSourcesHaveChanged(void *context)
which will be important later.
I tried to change :
IOPSNotificationCreateRunLoopSource(PowerSourcesHaveChanged, NULL);
by
IOPSNotificationCreateRunLoopSource(@selector(PowerSourcesHaveChanged),
NULL);
and void PowerSourcesHaveChanged()
by -(void)PowerSourcesHaveChanged
but my function is never called.
This won't work because IOPSNotificationCreateRunLoopSource is just
plain C (or perhaps C++) and is expecting a function pointer, not an
Objective-C selector. A selector isn't a function pointer; it's more
like the name of a function.
The secret is to figure out what the second "context" parameter to
IOPSNotificationCreateRunLoopSource is for. The documentation says
"Any user-defined pointer, passed to the IOPowerSource callback." So
what you would do is this:
IOPSNotificationCreateRunLoopSource(PowerSourcesHaveChanged, (void
*)self);
and then in your callback:
void PowerSourcesHaveChanged(void *context) {
id callbackObject = (id) context;
[callbackObject doSomething];
}
That's why callbacks on Mac OS X always have some sort of "context" or
"refcon" or "userinfo" argument.
-- Chris
--
Chris Hanson <email@hidden>
bDistributed.com, Inc.
Outsourcing Vendor Evaluation
Custom Mac OS X Development
Cocoa Developer Training
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.