IONotificationPortCreate function call generates compiler error

浪尽此生 提交于 2019-12-11 03:12:25

问题


I am having an issue with the IONotificationCreatePort function in IOKit:

var NotificationPort = IONotificationPortCreate(MasterPort)
IONotificationPortSetDispatchQueue(NotificationPort, DispatchQueue)

gives the following compiler error when NotificationPort is used in the function call in the second line

'Unmanaged IONotificationPort' is not identical to 'IONotificationPort'

if I use the following code based on the information in the Using Swift with Cocoa and Objective-C document, it compiles but generates a runtime error

var NotificationPort = IONotificationPortCreate(MasterPort).takeRetainedValue()
IONotificationPortSetDispatchQueue(NotificationPort, DispatchQueue)

Thread 1: EXC_BAD_ACCESS(code=1, address=0xwhatever)

So I think I have the run time error figured out, the IONotificationPort object does not have takeRetainedValue method

The crux of the problem as I see it, is that the IONotificationPortCreate function creates an IONotificationPort object and returns the reference to it.

I have looked all over the place and there is lots of information about and ways to pass references into a function call from Swift but nowhere can I find how to deal with references as a return value.

Can Swift call an object by reference?

Or am I way off the mark here????

Here is the objective C code that I am trying to convert to swift:

    _notificationPort = IONotificationPortCreate(masterPort);
    IONotificationPortSetDispatchQueue(_notificationPort, _controllerQueue);

Here is the complete code snippet from my swift file:

    //Get IOKit Master Port

    var MasterPort: mach_port_t = 0
    let BootstrapPort: mach_port_t = 0
    var MasterPortReturnCode: kern_return_t = 0

    MasterPortReturnCode = IOMasterPort(BootstrapPort, &MasterPort)
    println("Master port returned as \(MasterPort) with return code of \(MasterPortReturnCode)")

    //Set up notification port and send queue

    let DispatchQueue = dispatch_queue_create("com.apparata.AVB_Browser", DISPATCH_QUEUE_SERIAL)
    var NotificationPort = IONotificationPortCreate(MasterPort)
    IONotificationPortSetDispatchQueue(NotificationPort, DispatchQueue)

来源:https://stackoverflow.com/questions/27520774/ionotificationportcreate-function-call-generates-compiler-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!