Swift iOS API controller stopped working

三世轮回 提交于 2019-12-01 07:54:17

问题


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!


回答1:


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.


来源:https://stackoverflow.com/questions/25994608/swift-ios-api-controller-stopped-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!