gpt4 book ai didi

swift - 无法将类型 'UnsafePointer' 的值转换为预期参数类型 'UnsafePointer<_>'

转载 作者:行者123 更新时间:2023-11-30 11:37:18 24 4
gpt4 key购买 nike

我正在尝试更新我在 http://mattg411.com/swift-coremidi-callbacks/ 找到的 CoreMidi 示例中的代码

代码是 Swift 3 之前的代码,所以我需要做一些调整。问题是我基本上从来不需要和不安全的指针和 friend 一起玩。所以我想我已经成功解决了一些问题,但仍然存在一个问题并给我带来了这个错误 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 的修复

https://github.com/nissaba/Librarian

最佳答案

如果我正确理解代码,行

let msgPtr:UnsafePointer<MIDIObjectAddRemoveNotification> = UnsafePointer<MIDIObjectAddRemoveNotification>(message)

应将内存从 MIDINotification 重新绑定(bind)到 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)

关于swift - 无法将类型 'UnsafePointer<MIDINotification>' 的值转换为预期参数类型 'UnsafePointer<_>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49610871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com