簡體   English   中英

Swift iOS API控制器停止工作

[英]Swift iOS API controller stopped working

我正在使用一個文件來處理對API的調用,如下所示:

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)
            }
        })
    }
}

我稱呼它使用類似這樣的東西:

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

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

最近效果很好,但現在我的應用崩潰了,我在APIController中將此行突出顯示:

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

我能想到的唯一改變是我從移動4G互聯網切換到了WiFi。

在日志中,我得到: fatal error: unexpectedly found nil while unwrapping an Optional value

高亮顯示: Thread 5: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

無論我正在調用什么API,都會發生這種情況。 我正在運行Xcode 6.0.1,並且沒有做任何最新更新。

干杯!

很多人報告Xcode 6.0 GM和Wifi連接存在錯誤。

為了解決此問題,請嘗試以下步驟

  • 關閉模擬器
  • 關閉您的Xcode
  • 轉到您的DerviedData文件夾並刪除其下面的所有文件夾。 (〜/ Library / Developer / Xcode / DerivedData)不用擔心在Xcode中打開項目時會再次創建文件夾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM