在调用以下代码时,尝试将"self"传递给swift中的C函数:
var callbackStruct : AURenderCallbackStruct =
AURenderCallbackStruct.init(
inputProc: recordingCallback,
inputProcRefCon: UnsafeMutablePointer<Void>
)
Run Code Online (Sandbox Code Playgroud)
在这里将"self"转换为UnsafeMutablePointer类型的理想方法是什么?
在"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'或文字闭包的引用形成"
我想检测我的应用程序中插入/删除的特定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)