[英]Swift iOS API controller stopped working
I am using a file to handle my calls to my APIs which looks like this: 我正在使用一个文件来处理对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)
}
})
}
}
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: 最近效果很好,但现在我的应用崩溃了,我在APIController中将此行突出显示:
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. 我能想到的唯一改变是我从移动4G互联网切换到了WiFi。
In the log i get: fatal error: unexpectedly found nil while unwrapping an Optional value
在日志中,我得到:
fatal error: unexpectedly found nil while unwrapping an Optional value
The highlight says: Thread 5: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
高亮显示:
Thread 5: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
This occurs regardless of what API i'm calling. 无论我正在调用什么API,都会发生这种情况。 I'm running Xcode 6.0.1 and have not done any recent updates.
我正在运行Xcode 6.0.1,并且没有做任何最新更新。
Cheers! 干杯!
Lots of People are reporting a bug with Xcode 6.0 GM and Wifi connection. 很多人报告Xcode 6.0 GM和Wifi连接存在错误。
In order to resolve this issue try these steps 为了解决此问题,请尝试以下步骤
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.