[英]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连接存在错误。
为了解决此问题,请尝试以下步骤
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.