如果我有这样的方法:
func someMethod(contextPtr: UnsafeMutablePointer<Void>)
如何从 中获取对象contextPtr
?
func someMethod(contextPtr: UnsafeMutablePointer<Void>){
let object:MyObject = contextPtr.memory
}
给出:
'Void' 不能转换为 'MyObject'
秘方是什么
更多详情:
我实际上在这里做的是设置一个全局回调函数SCNetworkReachability
:
func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer<Void>) {
let r:Reachability = info.memory
}
然后添加回调如下:
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)