I have developed an iOS application in swift 2.0 and have used a class called Reachability to determine whether the user is connected to the internet or not. The application runs, however the compiler stops and outputs this error:
thread 1: exc_bad_instruction(code=exc_i386_invop,subcode=0x0)
here is the code, and error occurs where the comment is.
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)
}
}
Also I declared internetReachability like this
var internetReachability: Reachability!
Also this is how i Initialized internetReachability in another function called checkStatus()
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)
}
Do not understand what the error is and how to fix it. Help appreciated.