无法将类型 'UnsafePointer<MIDINotification>' 的值转换为预期的参数类型 'UnsafePointer<_>'
Cannot convert value of type 'UnsafePointer<MIDINotification>' to expected argument type 'UnsafePointer<_>'
我正在尝试从 http://mattg411.com/swift-coremidi-callbacks/
中找到的 CoreMidi 示例更新代码
而且代码是Swift3之前的日期,所以我需要做一些调整。
问题是我基本上从来不需要玩不安全的指针和朋友。所以我想我已经设法解决了一些问题,但其中一个仍然存在并让我得到这个错误 Cannot convert value of type 'UnsafePointer<MIDINotification>' to expected argument type 'UnsafePointer<_>'
给出这个错误的代码是 ...UnsafePointer<MIDIObjectAddRemoveNotification>(message)
此方法的一部分:
func MIDIUtil_MIDINotifyProc(message: UnsafePointer<MIDINotification>, refCon: UnsafeMutableRawPointer) -> Void
{
let notification:MIDINotification = message.pointee
if (notification.messageID == .msgObjectAdded || notification.messageID == .msgObjectRemoved)
{
let msgPtr:UnsafePointer<MIDIObjectAddRemoveNotification> = UnsafePointer<MIDIObjectAddRemoveNotification>(message)
let changeMsg:MIDIObjectAddRemoveNotification = msgPtr.pointee
let h:AnyObject = unbridgeMutable(ptr: refCon)
let handler:MIDICallbackHandler = h as! MIDICallbackHandler
handler.processMidiObjectChange(message: changeMsg)
}
}
编辑:
我根据在网上找到的一些教程创建了一个小项目。
包括来自 user28434
的修复
如果我正确理解代码,行
let msgPtr:UnsafePointer<MIDIObjectAddRemoveNotification> = UnsafePointer<MIDIObjectAddRemoveNotification>(message)
应该将内存从 MIDINotification
重新绑定到 MIDIObjectAddRemoveNotification
。
在 Swift 3.0+ 中你应该使用 withMemoryRebound(to:capacity:_:)
.
像这样:
let msgPtr:UnsafePointer<MIDIObjectAddRemoveNotification> = message.withMemoryRebound(to: MIDIObjectAddRemoveNotification.self, capacity: 1) { (pointer) in
return pointer
}
或者还有另一种方法:通过将 UnsafePointer
转换为 UnsafeRawPointer
然后 "assume memory bound":
let msgPtr:UnsafePointer<MIDIObjectAddRemoveNotification> = UnsafeRawPointer(message).assumingMemoryBound(to: MIDIObjectAddRemoveNotification.self)
我正在尝试从 http://mattg411.com/swift-coremidi-callbacks/
中找到的 CoreMidi 示例更新代码而且代码是Swift3之前的日期,所以我需要做一些调整。
问题是我基本上从来不需要玩不安全的指针和朋友。所以我想我已经设法解决了一些问题,但其中一个仍然存在并让我得到这个错误 Cannot convert value of type 'UnsafePointer<MIDINotification>' to expected argument type 'UnsafePointer<_>'
给出这个错误的代码是 ...UnsafePointer<MIDIObjectAddRemoveNotification>(message)
此方法的一部分:
func MIDIUtil_MIDINotifyProc(message: UnsafePointer<MIDINotification>, refCon: UnsafeMutableRawPointer) -> Void
{
let notification:MIDINotification = message.pointee
if (notification.messageID == .msgObjectAdded || notification.messageID == .msgObjectRemoved)
{
let msgPtr:UnsafePointer<MIDIObjectAddRemoveNotification> = UnsafePointer<MIDIObjectAddRemoveNotification>(message)
let changeMsg:MIDIObjectAddRemoveNotification = msgPtr.pointee
let h:AnyObject = unbridgeMutable(ptr: refCon)
let handler:MIDICallbackHandler = h as! MIDICallbackHandler
handler.processMidiObjectChange(message: changeMsg)
}
}
编辑: 我根据在网上找到的一些教程创建了一个小项目。 包括来自 user28434
的修复如果我正确理解代码,行
let msgPtr:UnsafePointer<MIDIObjectAddRemoveNotification> = UnsafePointer<MIDIObjectAddRemoveNotification>(message)
应该将内存从 MIDINotification
重新绑定到 MIDIObjectAddRemoveNotification
。
在 Swift 3.0+ 中你应该使用 withMemoryRebound(to:capacity:_:)
.
像这样:
let msgPtr:UnsafePointer<MIDIObjectAddRemoveNotification> = message.withMemoryRebound(to: MIDIObjectAddRemoveNotification.self, capacity: 1) { (pointer) in
return pointer
}
或者还有另一种方法:通过将 UnsafePointer
转换为 UnsafeRawPointer
然后 "assume memory bound":
let msgPtr:UnsafePointer<MIDIObjectAddRemoveNotification> = UnsafeRawPointer(message).assumingMemoryBound(to: MIDIObjectAddRemoveNotification.self)