此错误不断出现在我的代码中,我需要知道如何处理它。我还是个新手,所以如果你想给我一些关于如何在Swift中编码的技巧,我会很高兴看到它们
使用未解析的标识符'self'
这是我的代码:
import ReachabilitySwift
class ReachabilityManager: NSObject {
static let shared = ReachabilityManager()
}
var isNetworkAvailable : Bool {
return reachabilityStatus != .notReachable
}
var reachabilityStatus: Reachability.NetworkStatus = .notReachable
let reachability = Reachability()!
func reachabilityChanged(notification: Notification) {
let reachability = notification.object as! Reachability
switch reachability.currentReachabilityStatus {
case .notReachable:
debugPrint("Network became unreachable")
case .reachableViaWiFi:
debugPrint("Network reachable through WiFi")
case .reachableViaWWAN:
debugPrint("Network reachable through Cellular Data")
}
}
func startMonitoring() {
NotificationCenter.default.addObserver(self,selector: #selector(self.reachabilityChanged),name: ReachabilityChangedNotification,object: reachability)
do{
try reachability.startNotifier()
} catch {
debugPrint("Could not start reachability notifier")
}
}
答案 0 :(得分:0)
如果未定义self
,则会发生这种情况,请确保您尝试将startMonitoring()
函数添加到类,扩展或结构范围。