Getting this error: thread 1: exc_bad_instruction(code=exc_i386_invop,subcode=0x0)
Getting this error: thread 1: exc_bad_instruction(code=exc_i386_invop,subcode=0x0)
我在 swift 2.0 中开发了一个 iOS 应用程序,并使用了一个名为 Reachability 的 class 来确定用户是否连接到互联网。应用程序运行,但编译器停止并输出此错误:
thread 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?
并在整个代码中处理可选值
- 或 确保在尝试访问
internetReachability
. 之前调用方法 checkInternetConnection
幸运的是,通过简单地重新排列您的代码,这里为您提供了最后一个选项的简单版本:
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
尝试访问它们之前都设置了三种可达性类型。
我在 swift 2.0 中开发了一个 iOS 应用程序,并使用了一个名为 Reachability 的 class 来确定用户是否连接到互联网。应用程序运行,但编译器停止并输出此错误:
thread 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?
并在整个代码中处理可选值 - 或 确保在尝试访问
internetReachability
. 之前调用方法
checkInternetConnection
幸运的是,通过简单地重新排列您的代码,这里为您提供了最后一个选项的简单版本:
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
尝试访问它们之前都设置了三种可达性类型。