您的问题是否特别与IOKit回调例程有关?你给出的具体例子的问题是IOServiceMatchingCallback只需要2个参数,而不是3.你需要你的RawDeviceAdded()和BulkTestDeviceAdded()回调函数来匹配IOServiceMatchingCallback原型并接受self作为第一个参数(refCon),而不是第三个.此外,您需要传入self作为IOServiceAddMatchingNotification()的倒数第二个参数,以便通过回调将其传递给您.
在Objective-C代码中处理C回调的常用方法就是使用一个静态函数将回调转发给您的实例.因此,您的示例回调代码如下所示:
static RawDeviceAdded(void* refcon, io_iterator_t iterator) { [(MyClass*)refcon rawDeviceAdded:iterator]; } @implementation MyClass - (void)setupCallbacks { // ... all preceding setup snipped kr = IOServiceAddMatchingNotification(gNotifyPort,kIOFirstMatchNotification, matchingDict,RawDeviceAdded,(void*)self,&gRawAddedIter ); // call the callback method once to 'arm' the iterator [self rawDeviceAdded:gRawAddedIterator]; } - (void)rawDeviceAdded:(io_iterator_t)iterator { // take care of the iterator here, making sure to complete iteration to re-arm it } @end