Hello!
I am trying to write an application for macOS. I am using Cocoa and Swift and targeting macOS 10.13.
My goal is to open a USB HID device. I am using IOKit.hid
Summary of issue:
let devices = IOHIDManagerCopyDevices(manager)
if (devices != nil) {
var refs =<unsaferawpointer?>
UnsafeMutablePointer<unsaferawpointer?>.allocate(capacity: CFSetGetCount(devices))
CFSetGetValues(devices, refs)
var deviceHandle = refs![0]
}
- IOHIDManagerCopyDevices(...) gives me a CFSetRef
- I unpack the CFSetRef using CFSetGetValues(...); the second argument is of type UnsafeMutablePointer<UnsafeRawPointer?>?
- Now, I want to get the first item, call it deviceHandle
- deviceHandle is of type UnsafeRawPointer? ........ but I want an IOHIDDevice😟
I'm still very new to development for Apple platforms so please let me know if I'm missing anything obvious
But I'm stumped as to how to convert my UnsafeRawPointer? to an IOHIDDevice.
The below gives me an EXC_BAD_INSTRUCTION (EXC_I386_INVOP):
var test = deviceHandle as! IOHIDDevice
I have already confirmed that the pointer's _rawValue is correct by comparing with another implementation using IOHIDManagerRegisterDeviceMatchingCallback(), but I would prefer to do this synchronously
Is there an easy way to convert my UnsafeRawPointer? to IOHIDDevice?
Thanks
Kevin