相关疑难解决方法(0)

如何在Swift中将self转换为UnsafeMutablePointer <Void>类型

在调用以下代码时,尝试将"self"传递给swift中的C函数:

var callbackStruct : AURenderCallbackStruct = 
    AURenderCallbackStruct.init(
      inputProc: recordingCallback,
      inputProcRefCon: UnsafeMutablePointer<Void>
    )
Run Code Online (Sandbox Code Playgroud)

在这里将"self"转换为UnsafeMutablePointer类型的理想方法是什么?

pointers casting unsafe-pointers swift

44
推荐指数
1
解决办法
2万
查看次数

如何使用实例方法作为只接受func或文字闭包的函数的回调

在"ViewController.swift"中我创建了这个回调:

func callback(cf:CFNotificationCenter!, 
    ump:UnsafeMutablePointer<Void>, 
    cfs:CFString!, 
    up:UnsafePointer<Void>, 
    cfd:CFDictionary!) -> Void {

}
Run Code Online (Sandbox Code Playgroud)

使用此观察者:

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), 
    nil, 
    self.callback, 
    "myMESSage", 
    nil, 
    CFNotificationSuspensionBehavior.DeliverImmediately)
Run Code Online (Sandbox Code Playgroud)

导致此编译器错误:

"AC函数指针只能通过对'func'或文字闭包的引用形成"

callback swift

26
推荐指数
1
解决办法
1万
查看次数

如何在Swift中实现IOServiceMatchingCallBack

我想检测我的应用程序中插入/删除的特定USB.现在,我可以通过本教程使用USB设备接口获取deviceName .但是,如何在Swift 中执行(deviceAdded)IOServiceMatchingCallBack的回调函数.

我尝试如下,但我收到一个错误:无法将类型'(UnsafePointer,iterator:io_iterator_t) - >()'的值转换为预期的参数类型'IOServiceMatchingCallback!'

func detectUSBEvent() {
    var portIterator: io_iterator_t = 0
    var kr: kern_return_t = KERN_FAILURE
    let matchingDict = IOServiceMatching(kIOUSBDeviceClassName)

    let vendorIDString = kUSBVendorID as CFStringRef!
    let productIDString = kUSBProductID as CFStringRef!
    CFDictionarySetValue(matchingDict, unsafeAddressOf(vendorIDString), unsafeAddressOf(VendorID))
    CFDictionarySetValue(matchingDict, unsafeAddressOf(productIDString), unsafeAddressOf(ProductID))

    // To set up asynchronous notifications, create a notification port and add its run loop event source to the program’s run loop
    let gNotifyPort: IONotificationPortRef = IONotificationPortCreate(kIOMasterPortDefault)
    let runLoopSource: Unmanaged<CFRunLoopSource>! = IONotificationPortGetRunLoopSource(gNotifyPort)
    let gRunLoop: …
Run Code Online (Sandbox Code Playgroud)

macos usb iokit swift

2
推荐指数
1
解决办法
2082
查看次数

标签 统计

swift ×3

callback ×1

casting ×1

iokit ×1

macos ×1

pointers ×1

unsafe-pointers ×1

usb ×1