【发布时间】:2016-01-25 21:13:55
【问题描述】:
以下代码出现错误,我该如何解决?
'调用可以抛出,但没有标记'try',错误未处理'
let reachability = Reachability.reachabilityForInternetConnection() // - Error Here
reachability.whenReachable = { reachability in
if reachability.isReachableViaWiFi() {
let alertController = UIAlertController(title: "Alert", message: "Reachable via WiFi", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
else {
let alertController = UIAlertController(title: "Alert", message: "Reachable via Cellular", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
}
reachability.whenUnreachable = { reachability in
let alertController = UIAlertController(title: "Alert", message: "Not Reachable", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
reachability.startNotifier() // - Error Here
}
【问题讨论】:
-
你看过github.com/ashleymills/Reachability.swift上的“示例 - 闭包”吗?
-
@Leo Dabus 您能否更新此问题中的以下代码,谢谢,目前出现错误。
-
@GurvierSinghDhillon 尝试先阅读 Martin R 刚刚为您发布的链接
-
@Mark R 我可以使用您提供的链接获得该方法,但是我想要在这里实现的是显示一个 AlertController,无论它们是否是互联网连接。
标签: xcode swift viewcontroller