我在线收到 iOS 13 (swift 5) 的警告 xcode
Cannot force unwrap value of non-optional type 'Reachability'
11.1let reachability = Reachability()!
我试过了,如果语句和尝试做,但似乎没有一个工作。删除!给出警告“调用可以抛出,但不能从属性初始化器中抛出错误”
import Reachability
class ReachabilityDetect {
let reachability = Reachability()!
var dm = DataModel()
func addObservers(datamodel: DataModel) {
self.dm = datamodel
NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
do{
try reachability.startNotifier()
}catch{
Util.DLog("Reachability notifier niet te starten.")
}
}
@objc func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
switch reachability.connection {
case .wifi:
Util.DLog("WiFi is actief")
self.dm.dataConnectionisWifi = true
self.dm.dataConnectionisCellular = false
case .cellular:
Util.DLog("Celluar data is actief")
self.dm.dataConnectionisWifi = false
self.dm.dataConnectionisCellular = true
case .none:
Util.DLog("geen celluar of wifi data actief")
self.dm.dataConnectionisWifi = false
self.dm.dataConnectionisCellular = false
default: break
}
}
}