2

I am using a file to handle my calls to my APIs which looks like this:

import UIKit

protocol APIControllerProtocol {
    func JSONAPIResults(results: NSArray)

}

class APIController: NSObject {
    var delegate:APIControllerProtocol?

    func GetAPIResultsAsync(urlString:String, elementName:String) {

        //The Url that will be called
        var url = NSURL.URLWithString(urlString)
        //Create a request
        var request: NSURLRequest = NSURLRequest(URL: url)
        //Create a queue to hold the call
        var queue: NSOperationQueue = NSOperationQueue()

        // Sending Asynchronous request using NSURLConnection
        NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{(response:NSURLResponse!, responseData:NSData!, error: NSError!) ->Void in
            var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
            //Serialize the JSON result into a dictionary
            let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary

            //If there is a result add the data into an array
            if jsonResult.count>0 && jsonResult["\(elementName)"]?.count > 0 {

                var results: NSArray = jsonResult["\(elementName)"] as NSArray
                //Use the completion handler to pass the results
                self.delegate?.JSONAPIResults(results)

            } else {

                println(error)
            }
        })
    }
}

I am calling it using something similar to this:

var APIBaseUrl: String = "http://***.se/**/**.php"
        var urlString:String = "\(APIBaseUrl)"

        self.api.delegate = self
        api.GetAPIResultsAsync(urlString, elementName:"groupActivities")

This have recently worked great but now my app crashes and i get this row in the APIController highlighted:

let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(responseData, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary

The only thing that i can think of that have changed is that i switched from mobile 4G internet to my WiFi.

In the log i get: fatal error: unexpectedly found nil while unwrapping an Optional value

The highlight says: Thread 5: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

This occurs regardless of what API i'm calling. I'm running Xcode 6.0.1 and have not done any recent updates.

Cheers!

Nils Wasell
  • 987
  • 1
  • 8
  • 10
  • People are reporting a bug with Xcode 6GM and Wifi connection. Close, Xcode & simulator, cleanup DerviedData folder and then try again. http://stackoverflow.com/questions/25372318/error-domain-nsurlerrordomain-code-1005-the-network-connection-was-lost – Sam B Sep 23 '14 at 12:42
  • Worked great, please post as answer. Though the same error show up at another line but i belive this is some other error in my code since the other API calls is working fine. Thanks a bunch! @SamBudda – Nils Wasell Sep 23 '14 at 12:52
  • 2
    Is there a solution if this is occurring on a device? – Kaili Oct 17 '14 at 17:44

1 Answers1

5

Lots of People are reporting a bug with Xcode 6.0 GM and Wifi connection.

In order to resolve this issue try these steps

  • Close your Simulator
  • Close your Xcode
  • Go to your DerviedData folder and remove all folders underneath it. (~/Library/Developer/Xcode/DerivedData) Don't worry the folders will be created again when you open your project in Xcode.
Sam B
  • 27,273
  • 15
  • 84
  • 121
  • 1
    Same, this didn't work for me either. I'm assuming this only works for Simulator? @Karthik, are you having this issuing on a device, because that's my issue – MoMo Jan 08 '15 at 16:04
  • @MoMo were you able to fix it? – nr5 Sep 05 '17 at 06:11