ios - Swift iOS API Controller 停止工作

标签 ios json swift nsjsonserialization

我正在使用一个文件来处理对 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 :在解包可选值时意外发现 nil

突出显示:线程 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 中打开项目时会再次创建文件夹。

关于ios - Swift iOS API Controller 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25994608/

相关文章:

ios - 添加childViewController不包括背景颜色等基本设置

ios - 在 SwiftyJson 中过滤数据

ios - 将点击操作添加到 NavigationController 中的自定义 View

ios - 如何在不截断 ios 的情况下查看 "log stream"日志消息

ios - JavaScriptCore 除了 'hello, world' 之外不工作

ios - 警告 : "Sending ' NSObject *' to parameter of incompatible type ' id<NSCopying>'

ios - 如何在不破坏旧应用程序版本的情况下更新 Apple 托管的内容?

ios - 从 iOS 应用程序使用用户名打开 Vine 配置文件

javascript - 如果我的 JSON 代码使用自己的输出(来自 JSON.stringify),为什么它会返回 "syntax error"?

json - 如何形成 ASP.NET OData WebApi 的 PATCH 正文?