我正在使用一个文件来处理对 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/