得到此错误:线程1:exc_bad_instruction(code = exc_i386_invop,subcode = 0x0)

时间:2016-01-28 19:18:52

标签: ios swift reachability

我在swift 2.0中开发了一个iOS应用程序,并使用了一个名为Reachability的类来确定用户是否连接到互联网。应用程序运行,但编译器停止并输出此错误:

  

主题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)
}

不明白错误是什么以及如何解决。帮助赞赏。

1 个答案:

答案 0 :(得分:1)

代码在给定行上崩溃,因为您将变量声明为隐式展开可选,这意味着在此情况下将值解包以便能够将其与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尝试访问它们之前设置了三种可访问性类型。