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

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

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

var callbackStruct : AURenderCallbackStruct = 
    AURenderCallbackStruct.init(
      inputProc: recordingCallback,
      inputProcRefCon: UnsafeMutablePointer<Void>
    )

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


holdtom
浏览 1093回答 3
3回答

慕的地6264312

一个对象的指针(即,实例引用类型)可以被转换为一个UnsafePointer<Void>(的夫特映射const void *,UnsafeRawPointer在夫特3)和背部。在Objective-C中你会写void&nbsp;*voidPtr&nbsp;=&nbsp;(__bridge&nbsp;void*)self;//&nbsp;MyType&nbsp;*mySelf&nbsp;=&nbsp;(__bridge&nbsp;MyType&nbsp;*)voidPtr;(有关这些演员阵容的确切含义,请参阅Clang ARC文档中的3.2.4桥接演员表。)Swift有一种Unmanaged用于此目的的类型。使用起来有点麻烦,因为它可以COpaquePointer&nbsp;代替使用UnsafePointer<Void>。这里有两个辅助方法(以Objective-C&nbsp;__bridge强制转换命名):func&nbsp;bridge<T&nbsp;:&nbsp;AnyObject>(obj&nbsp;:&nbsp;T)&nbsp;->&nbsp;UnsafePointer<Void>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;UnsafePointer(Unmanaged.passUnretained(obj).toOpaque()) &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;return&nbsp;unsafeAddressOf(obj)&nbsp;//&nbsp;***}func&nbsp;bridge<T&nbsp;:&nbsp;AnyObject>(ptr&nbsp;:&nbsp;UnsafePointer<Void>)&nbsp;->&nbsp;T&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;Unmanaged<T>.fromOpaque(COpaquePointer(ptr)).takeUnretainedValue() &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;return&nbsp;unsafeBitCast(ptr,&nbsp;T.self)&nbsp;//&nbsp;***}“复杂”表达式只是满足Swifts严格类型系统所必需的。在编译的代码中,这只是指针之间的转换。(***如果您愿意使用“不安全”方法,可以在注释中指示更短,但编译后的代码是相同的。)使用此辅助方法,您可以将其传递self给C函数&nbsp;let&nbsp;voidPtr&nbsp;=&nbsp;bridge(self)(或者UnsafeMutablePointer<Void>(bridge(self))如果C函数需要一个可变指针),并将其转换回对象指针 - 例如在回调函数中 -&nbsp;let&nbsp;mySelf&nbsp;:&nbsp;MyType&nbsp;=&nbsp;bridge(voidPtr)不会发生所有权转移,因此self&nbsp;只要使用了void指针,就必须确保存在。并且为了完整起见,Swift等同于Objective-C&nbsp;__bridge_retained和__bridge_transferfunc&nbsp;bridgeRetained<T&nbsp;:&nbsp;AnyObject>(obj&nbsp;:&nbsp;T)&nbsp;->&nbsp;UnsafePointer<Void>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;UnsafePointer(Unmanaged.passRetained(obj).toOpaque())}func&nbsp;bridgeTransfer<T&nbsp;:&nbsp;AnyObject>(ptr&nbsp;:&nbsp;UnsafePointer<Void>)&nbsp;->&nbsp;T&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;Unmanaged<T>.fromOpaque(COpaquePointer(ptr)).takeRetainedValue()}bridgeRetained()将对象指针强制转换为void指针并保留该对象。bridgeTransfer()将void指针转换回对象指针并使用retain。优点是在调用之间不能释放对象,因为保持了强引用。缺点是调用必须适当平衡,并且很容易导致保留周期。更新Swift 3(Xcode 8):func&nbsp;bridge<T&nbsp;:&nbsp;AnyObject>(obj&nbsp;:&nbsp;T)&nbsp;->&nbsp;UnsafeRawPointer&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;UnsafeRawPointer(Unmanaged.passUnretained(obj).toOpaque())}func&nbsp;bridge<T&nbsp;:&nbsp;AnyObject>(ptr&nbsp;:&nbsp;UnsafeRawPointer)&nbsp;->&nbsp;T&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;Unmanaged<T>.fromOpaque(ptr).takeUnretainedValue()}func&nbsp;bridgeRetained<T&nbsp;:&nbsp;AnyObject>(obj&nbsp;:&nbsp;T)&nbsp;->&nbsp;UnsafeRawPointer&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;UnsafeRawPointer(Unmanaged.passRetained(obj).toOpaque())}func&nbsp;bridgeTransfer<T&nbsp;:&nbsp;AnyObject>(ptr&nbsp;:&nbsp;UnsafeRawPointer)&nbsp;->&nbsp;T&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;Unmanaged<T>.fromOpaque(ptr).takeRetainedValue()}对“不安全指针”的相关更改在中描述SE-0017更改Unmanaged以使用UnsafePointerSE-0107 UnsafeRawPointer API

梵蒂冈之花

在我看来,这是什么withUnsafeMutablePointer- 将任意Swift指针转换为C指针。所以大概你可以做到这一点(我没试过,但我测试的代码安全地工作):var&nbsp;mself&nbsp;=&nbsp;self&nbsp;withUnsafeMutablePointer(&mself)&nbsp;{&nbsp;v&nbsp;in &nbsp;&nbsp;&nbsp;&nbsp;let&nbsp;v2&nbsp;=&nbsp;UnsafeMutablePointer<Void>(v) &nbsp;&nbsp;&nbsp;&nbsp;myStruct.inputProcRefCon&nbsp;=&nbsp;v2}

一只名叫tom的猫

func bridge<T : AnyObject>(obj : T) -> UnsafePointer<Void> {return UnsafePointer(Unmanaged.passUnretained(obj).toOpaque())}func bridge<T : AnyObject>(ptr : UnsafePointer<Void>) -> T {return Unmanaged<T>.fromOpaque(ptr).takeUnretainedValue()}func bridgeRetained<T : AnyObject>(obj : T) -> UnsafePointer<Void> {return UnsafePointer( Unmanaged.passRetained(obj).toOpaque())}func bridgeTransfer<T : AnyObject>(ptr : UnsafePointer<Void>) -> T {return Unmanaged<T>.fromOpaque(ptr).takeRetainedValue()}
打开App,查看更多内容
随时随地看视频慕课网APP