由于Swift 2.3到Swift 3.0的转换引发了很多问题,我试图解决这个问题,但到目前为止还没有得到解决方案。
无法转换类型'的值(SCNetworkReachability,flags: SCNetworkReachabilityFlags,info:UnsafeMutablePointer) - > ()' 预期的参数类型' SCNetworkReachabilityCallBack?'
这是我的代码:
func callback(_ reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer<Void>) {
let reachability = Unmanaged<Reachability>.fromOpaque(info).takeUnretainedValue()
DispatchQueue.main.async {
reachability.reachabilityChanged(flags)
}
}
在startNotifier
函数中,我们传递callback
,但它会生成错误。
public func startNotifier() throws {
guard !notifierRunning else { return }
var context = SCNetworkReachabilityContext(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
context.info = UnsafeMutablePointer(Unmanaged.passUnretained(self).toOpaque())
//THIS LINE GENERATES ERROR WARNING
if !SCNetworkReachabilitySetCallback(reachabilityRef!, callback, &context) {
stopNotifier()
throw ReachabilityError.unableToSetCallback
}
if !SCNetworkReachabilitySetDispatchQueue(reachabilityRef!, reachabilitySerialQueue) {
stopNotifier()
throw ReachabilityError.unableToSetDispatchQueue
}
// Perform an intial check
reachabilitySerialQueue.async { () -> Void in
let flags = self.reachabilityFlags
self.reachabilityChanged(flags)
}
notifierRunning = true
}
此代码在上述函数中生成错误。
//THIS LINE GENERATES ERROR WARNING
if !SCNetworkReachabilitySetCallback(reachabilityRef!, callback, &context) {
stopNotifier()
throw ReachabilityError.unableToSetCallback
}
我也经历了Martin的深度帖子,但没有得到解决方案。 任何帮助都应该是值得的。 在此先感谢。
答案 0 :(得分:2)
如果您在Swift 3中有异常,请务必检查the latest reference: (目前,最新的参考资料是最新的Xcode 8,beta 6.如果您使用的是beta 5或更早版本,则下面的代码不起作用。)
声明
typealias SCNetworkReachabilityCallBack = ( SCNetworkReachability, SCNetworkReachabilityFlags, UnsafeMutableRawPointer?) -> Void
回调的最后一个参数的类型已更改为UnsafeMutableRawPointer?
。
因此,您可能需要将callback
更改为以下内容:
func callback(_ reachability:SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutableRawPointer?) {
let reachability = Unmanaged<Reachability>.fromOpaque(info!).takeUnretainedValue()
DispatchQueue.main.async {
reachability.reachabilityChanged(flags)
}
}