实例成员不能用于闭包中的类型

时间:2017-01-16 05:44:16

标签: swift closures

我有一个像

这样的课程
class Main
{
var manager: IOHIDManager
var connectedDeviceList = [String]()
init()
{
    manager = IOHIDManagerCreate(kCFAllocatorDefault, IOOptionBits(kIOHIDOptionsTypeNone))
    IOHIDManagerSetDeviceMatching(manager, nil)
    IOHIDManagerRegisterDeviceMatchingCallback(manager, Handle_DeviceMatchingCallback, nil)
}
func Handle_DeviceMatchingCallback(_ context: UnsafeMutableRawPointer?, _ result: IOReturn, _ sender: UnsafeMutableRawPointer?, _ device: IOHIDDevice)
{
    connectedDeviceList.append(String(describing: device))
    print("Connected")
    connectedData() // just another function
}
}

我收到错误A C function pointer can only be formed from a reference to a 'func' or a literal closure

所以我试着写出像

这样的闭包
let Handle_DeviceMatchingCallback: IOHIDDeviceCallback = {context, result, sender, device in
    connectedDeviceList.append(String(describing: device))
    print("Connected")
    connectedData()
}

现在我在Instance member cannot be used on type 'Main'connectedData()

上获得了connectedDeviceList

当我删除那些实例变量和方法时,没有错误,但我需要实现该方法。

我该如何解决这个问题?

0 个答案:

没有答案