迅捷2-UnsafeMutablePointer< Void>反对 [英] Swift 2 - UnsafeMutablePointer<Void> to object
问题描述
如果我有类似的方法:
func someMethod(contextPtr: UnsafeMutablePointer<Void>)
如何从contextPtr
获取对象?
func someMethod(contextPtr: UnsafeMutablePointer<Void>){
let object:MyObject = contextPtr.memory
}
给予:
无效"不能转换为"MyObject"
'Void' is not convertible to 'MyObject'
秘诀是什么
更多细节:
我实际上在这里正在为SCNetworkReachability
设置全局回调函数:
What I'm actually doing here is setting up a global callback function for SCNetworkReachability
:
func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer<Void>) {
let r:Reachability = info.memory
}
,然后添加回调,如下所示:
and then adding the callback as follows:
var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
var s = self
withUnsafeMutablePointer(&s) {
context.info = UnsafeMutablePointer($0)
}
SCNetworkReachabilitySetCallback(reachability, callback, &context)
推荐答案
这应该起作用:将对象指针作为不透明的非托管指针传递 到回调:
This should work: pass the object pointer as an opaque unmanaged pointer to the callback:
context.info = UnsafeMutablePointer(Unmanaged.passUnretained(myObject).toOpaque())
SCNetworkReachabilitySetCallback(reachability, callback, &context)
并通过以下方式检索回调:
and retrieve in the callback via:
func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer<Void>) {
let myObject = Unmanaged<MyObject>.fromOpaque(COpaquePointer(info)).takeUnretainedValue()
}
当然,这假定存在对该对象的一些强引用 只要安装了回调,就不会 释放.
Of course this assumes that some strong reference to the object exists as long as the callback is installed, so that the object is not deallocated.
更新:请注意,这两种从对象指针到空指针的转换 如果您愿意使用不安全"功能,则可以简化往返操作:
Update: Note that both conversions from object pointer to void pointer and back can be simplified if you are willing to use "unsafe" functions:
context.info = unsafeAddressOf(myObject)
// ...
myObject = unsafeBitCast(info, MyObject.self)
据我所见,生成的汇编代码是相同的.
The generated assembly code is – as far as I can see – identical.
更新2:另请参见如何将自己投射到UnsafeMutablePointer< Void>快速输入以获取更多信息 关于桥接"和一些可以在这里使用的辅助功能.
Update 2: See also How to cast self to UnsafeMutablePointer<Void> type in swift for more information about the "bridging" and some helper functions which can be used here.
迅速3更新(Xcode 8 beta 6):
var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
context.info = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
// ...
func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) {
if let info = info {
let myObject = Unmanaged<MyObject>.fromOpaque(info).takeUnretainedValue()
// ...
}
}
这篇关于迅捷2-UnsafeMutablePointer< Void>反对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!