简体   繁体   中英

Swift iOS API controller stopped working

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM