无法为 swift 强制解包非可选类型 'Reachability' 的值 5
Cannot force unwrap value of non-optional type 'Reachability' for swift 5
我收到警告 xcode 11.1 for iOS 13 (swift 5)
Cannot force unwrap value of non-optional type 'Reachability'
线上 let reachability = Reachability()!
我试过了,if语句和try do,但是none好像work.Removing了!发出警告 "Call can throw, but errors cannot be thrown out of a property initializer"
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
}
}
}
我遇到了同样的问题。
而不是
let reachability = Reachability()!
使用这个
let reachability = try! Reachability()
我收到警告 xcode 11.1 for iOS 13 (swift 5)
Cannot force unwrap value of non-optional type 'Reachability'
线上 let reachability = Reachability()!
我试过了,if语句和try do,但是none好像work.Removing了!发出警告 "Call can throw, but errors cannot be thrown out of a property initializer"
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
}
}
}
我遇到了同样的问题。
而不是
let reachability = Reachability()!
使用这个
let reachability = try! Reachability()