I am making an app that connects to an API through Alamofire and SwiftyJSON, which is working fine when the internet is OK. However, when I simulate no internet by turning off my mac's wi-fi, it gives me a fatal error on lines related to IBOutlets that are completely unrelated to the online query, and don't require internet connection, namely, I am getting 'found nil while unwrapping' on:
for index in 0..<labels.count {
labels[index].textColor = ORANGE
}
and
let hash = String(NSUserDefaults.standardUserDefaults().floatForKey("hash"))
hR.text = hash //This line throws an error
which are two lines that absolutely don't require internet.
An example of the code that is connecting to the API is:
static func getExchange(handleComplete:(dataReturn:AnyObject?)->()) {
Alamofire.request(.GET, URL { (_, _, result) in
switch result {
case .Success(let data):
let json = JSON(data)
let usd = json["USD"]["last"].float
print("USD Exchange Rate is: \(usd!)")
NSUserDefaults.standardUserDefaults().setFloat(usd!, forKey: "currency")
handleComplete(dataReturn: usd)
case .Failure(_, let error):
print(error)
//CALL A FUNCTION IN VIEW CONTROLLER THAT TRIGGERS AN ALERT
ViewController().presentError()
}
}
}
Thanks in advance for your help!