gpt4 book ai didi

ios - SCNetworkReachabilityCallBack? Swift3.0 的类型转换问题

转载 作者:搜寻专家 更新时间:2023-11-01 07:20:32 25 4
gpt4 key购买 nike

<分区>

由于 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
}

enter image description here

我还深入了解了 Martin 的这篇帖子,但没有得到解决方案。任何帮助都应该是可观的。提前致谢。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com