SCNetworkReachabilityCallBack? Swift3.0 的类型转换问题
SCNetworkReachabilityCallBack? type conversion issue with Swift3.0
由于 Swift 2.3 到 Swift 3.0 的转换引发了很多问题,我正在尝试解决这个问题,但目前还没有得到解决方案。
Cannot convert value of type '(SCNetworkReachability, flags:
SCNetworkReachabilityFlags, info: UnsafeMutablePointer) -> ()'
to expected argument type '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
}
我也深入 post ,但没有得到解决方案。
任何帮助都应该是可观的。
提前致谢。
如果您在 Swift 3 中有什么奇怪的地方,请务必检查 the latest reference:
(至于目前,最新的参考是最新的 Xcode 8, beta 6。如果您使用的是 beta 5 或更早的版本,下面的代码将不起作用。)
Declaration
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)
}
}
由于 Swift 2.3 到 Swift 3.0 的转换引发了很多问题,我正在尝试解决这个问题,但目前还没有得到解决方案。
Cannot convert value of type '(SCNetworkReachability, flags: SCNetworkReachabilityFlags, info: UnsafeMutablePointer) -> ()' to expected argument type '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
}
我也深入 post
如果您在 Swift 3 中有什么奇怪的地方,请务必检查 the latest reference: (至于目前,最新的参考是最新的 Xcode 8, beta 6。如果您使用的是 beta 5 或更早的版本,下面的代码将不起作用。)
Declaration
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)
}
}