I have a program on HID. But it is written in Objective-C. It is a project and my friends only know swift. So I thought of converting it to swift and I can't figure out how to write that function.
Obj-c Code:
/* Creating The HID Manager */
IOHIDManagerRef manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
/* Dictionary Matching - All the devices */
IOHIDManagerSetDeviceMatching(manager,NULL);
/* Connected and Disconnected Call Backs */
IOHIDManagerRegisterDeviceMatchingCallback(manager, &Handle_DeviceMatchingCallback , NULL);
IOHIDManagerRegisterDeviceRemovalCallback(manager, &Handle_DeviceRemovalCallback,NULL);
And the call back functions are:
static void Handle_DeviceMatchingCallback(void *inContext,
IOReturn inResult,
void *inSender,
IOHIDDeviceRef inIOHIDDeviceRef)
{
printf("Connected\n");
}
static void Handle_DeviceRemovalCallback(void *inContext,
IOReturn inResult,
void *inSender,
IOHIDDeviceRef inIOHIDDeviceRef)
{
printf("Disconnected\n");
}
In swift,
I wrote Handle_DeviceMatchingCallback()
as
func Handle_DeviceMatchingCallback(inContext: UnsafeMutableRawPointer!, inResult: IOReturn, inSender: UnsafeMutableRawPointer!, inIOHIDDeviceRef: IOHIDDevice)
{
print("Connected")
}
But I can't pass the function to IOHIDManagerRegisterDeviceMatchingCallback()
.
How to do that?
One way to handle your case is making the callbacks as closures:
let Handle_DeviceMatchingCallback: IOHIDDeviceCallback = {context, result, sender, device in
print("Connected")
}
let Handle_DeviceRemovalCallback: IOHIDDeviceCallback = {context, result, sender, device in
print("Disconnected")
}
Or else, you need to declare callback functions with exactly the same signature as defined in IOHIDDeviceCallback
.
typealias IOHIDDeviceCallback = (UnsafeMutableRawPointer?, IOReturn, UnsafeMutableRawPointer?, IOHIDDevice) -> Void
(Taken from the Quick Help of Xcode.)
func Handle_DeviceMatchingCallback(_ context: UnsafeMutableRawPointer?, _ result: IOReturn, _ sender: UnsafeMutableRawPointer?, _ device: IOHIDDevice) {
print("Connected")
}
func Handle_DeviceRemovalCallback(_ context: UnsafeMutableRawPointer?, _ result: IOReturn, _ sender: UnsafeMutableRawPointer?, _ device: IOHIDDevice) {
print("Disconnected")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.