Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: RunLoop and callback function



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.

References: 
 >RunLoop and callback function (From: Thierry Bucco <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.