0

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!

Jacobo Koenig
  • 11,728
  • 9
  • 40
  • 75
  • Can you get a backtrace? – bbum Dec 06 '15 at 02:19
  • Sorry. Im kind of new to swift, can you please explain what you mean by a backtrace? – Jacobo Koenig Dec 06 '15 at 02:20
  • 1
    When you have this crash, you should find out which object is `nil` and is causing the crash. This can be done with the debug navigator. Plenty of questions about it on SO. [my question about a similar issue.](http://stackoverflow.com/questions/27629747/swift-trace-unexpectedly-found-nil-to-the-source) One of the answers has good screenshots. – R Menke Dec 06 '15 at 03:14
  • 1
    @RMenke Duped to R. Menke's question which, with the cleanup, deserves upvotes. – bbum Dec 06 '15 at 19:30

0 Answers0