【发布时间】:2016-11-15 17:27:48
【问题描述】:
我有我在这里找到的这段代码。现在我想知道如何在单例中使用它。我的理解是,如果我在单例中使用此代码,我会注意到网络状态是否发生变化。
func startNetworkReachabilityObserver() {
let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.google.com")
reachabilityManager?.listener = { status in
switch status {
case .NotReachable:
print("The network is not reachable")
case .Unknown :
print("It is unknown whether the network is reachable")
case .Reachable(.EthernetOrWiFi):
print("The network is reachable over the WiFi connection")
case .Reachable(.WWAN):
print("The network is reachable over the WWAN connection")
}
}
// start listening
reachabilityManager?.startListening()
}
【问题讨论】:
-
您可以简单地将其设为类方法并按原样使用它。我猜不需要使用单例。只要应用程序启动就调用这个函数。
-
在单例中使用它与在其他类中使用它没有区别,单例中没有魔法意味着它必须进入单例,只要该类的生命周期与您一样长,任何类都可以需要更改通知。
-
如果我创建一个类方法,我不会收到任何网络状态更改的通知。我会单身。
标签: ios singleton swift3 alamofire