我在swift 2.0中开发了一个iOS应用程序,并使用了一个称为“可达性”的类来确定用户是否连接到互联网。应用程序运行,但是编译器停止并输出此错误:
线程1:exc_bad_instruction(code = exc_i386_invop,subcode = 0x0)
这是代码,注释所在的位置会发生错误。
func updateInterfaceWithReachability (reachability: Reachability) {
if reachability == self.hostReachability
{
self.checkStatus(reachability)
let netStatus: NetworkStatus = reachability.currentReachabilityStatus()
let connectionRequired: Bool = reachability.connectionRequired()
var baseLabelText: NSString = ""
if connectionRequired
{
baseLabelText = NSLocalizedString("Cellular data network is available.\nInternet traffic will be routed through it after a connection is established.", comment: "Reachability text if a connection is required")
}
else
{
baseLabelText = NSLocalizedString("Cellular data network is active.\nInternet traffic will be routed through it.", comment: "Reachability text if a connection is not required")
}
}
if reachability == self.internetReachability // error displayed here
{
self.checkStatus(reachability)
}
if reachability == self.wifiReachability
{
self.checkStatus(reachability)
}
}
我也这样声明了internetReachability
var internetReachability: Reachability!
这也是我在另一个称为checkStatus()的函数中初始化internetReachability的方式
func checkInternetConnection () {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("reachabilityChanged"), name: kReachabilityChangedNotification, object: nil)
let remoteHostName: String = "www.apple.com"
self.hostReachability = Reachability.init(hostName: remoteHostName)
self.hostReachability.startNotifier()
self.updateInterfaceWithReachability(self.hostReachability)
self.internetReachability = Reachability.reachabilityForInternetConnection()
self.internetReachability.startNotifier()
self.updateInterfaceWithReachability(self.internetReachability)
self.wifiReachability = Reachability.reachabilityForLocalWiFi()
self.wifiReachability.startNotifier()
self.updateInterfaceWithReachability(self.wifiReachability)
}
不了解错误是什么以及如何解决。帮助表示赞赏。
最佳答案
代码在给定的行崩溃,因为您将变量声明为隐式解包的可选变量,这意味着在这种情况下该值将被解包,以便能够将其与reachability
进行比较。但是,由于您尚未为internetReachability
设置任何内容,因此它仍然是nil
,并且由于无法解开nil
而导致代码崩溃。
要解决此问题,您必须要么
nil
var internetReachability: Reachability?
,并在代码checkInternetConnection
之前,请确保已调用internetReachability
方法。 幸运的是,这里只需为您提供最后一个选项的简单版本,只需重新排列代码即可:
func checkInternetConnection () {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("reachabilityChanged"), name: kReachabilityChangedNotification, object: nil)
let remoteHostName: String = "www.apple.com"
self.hostReachability = Reachability.init(hostName: remoteHostName)
self.internetReachability = Reachability.reachabilityForInternetConnection()
self.wifiReachability = Reachability.reachabilityForLocalWiFi()
self.hostReachability.startNotifier()
self.internetReachability.startNotifier()
self.wifiReachability.startNotifier()
self.updateInterfaceWithReachability(self.internetReachability)
self.updateInterfaceWithReachability(self.wifiReachability)
self.updateInterfaceWithReachability(self.hostReachability)
}
这样,您可以确保在
self.updateInterfaceWithReachability
尝试访问这三种可达性类型之前,已全部设置好。关于ios - 收到此错误:线程1:exc_bad_instruction(code = exc_i386_invop,subcode = 0x0),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35070103/