Reputation: 33
How should I write this code in objective-c:
kr = IOServiceAddMatchingNotification(gNotifyPort,
kIOFirstMatchNotification, matchingDict, RawDeviceAdded, NULL, &gRawAddedIter);
I tried this one:
kr = IOServiceAddMatchingNotification(gNotifyPort, kIOFirstMatchNotification,
matchingDict, @selector(RawDeviceAdded), NULL, &gRawAddedIter);
and the function look like:
(void)RawDeviceAdded:(void *)refCon iterator:(io_iterator_t)iterator
{
.....
}
I am not sure if its right.
Upvotes: 2
Views: 378
Reputation: 122381
Short answer is: You can't do that directly.
This is because IOKit is a C-API so any callback functions it requires must be C, and not Objective-C.
That's not to say that you can't mix C and Objective-C, and use the C callback function to trampoline to your Objective-C method. It's simply a matter of getting the reference to the class to the C callback function; in this particular case that's using refCon
.
SomeObjcClass.m:
// Objective-C method (note that refCon is not a parameter as that
// has no meaning in the Objective-C method)
-(void)RawDeviceAdded:(io_iterator_t)iterator
{
// Do something
}
// C callback "trampoline" function
static void DeviceAdded(void *refCon, io_iterator_t iterator)
{
SomeObjcClass *obj = (SomeObjcClass *)refCon;
[obj RawDeviceAdded:iterator];
}
- (void)someMethod
{
// Call C-API, using a C callback function as a trampoline
kr = IOServiceAddMatchingNotification(gNotifyPort,
kIOFirstMatchNotification,
matchingDict,
DeviceAdded, // trampoline function
self, // refCon to trampoline
&gAddedIter
);
}
Upvotes: 3