因此,我使用NSURLConnection
来从网站获取JSON数据,它可以正常工作,并将数据存储在搜索控制器/表格视图中。现在,当用户单击视图中的单元格时,我想选择一个新视图,并从网站加载另一组JSON数据。但是,在此新视图中,连接永远不会运行方法。这是我两次都在做的事(每次都在不同的视图控制器中)
func startConnection(){
var url: NSURL = NSURL(string: self.formatted)!
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.scheduleInRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode) // I tried to put this in based on suggestions from searches I did, it didn't help
connection.start()
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
self.data.appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection!){
var err: NSError
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
println(jsonResult)
}
从断点看,它永远不会运行:
func connection(connection: NSURLConnection!, didReceiveData data: NSData!)
我打电话startConnection()
的ViewDidLoad
也实施connection:didFailWithError:
。查看报告内容。没有这些,您将无法知道失败的原因。
func connection(connection: NSURLConnection, didFailWithError error: NSError) {
println("connection error = \(error)")
}
同样,在didReceiveData
(以及其他方法中),该NSURLConnection
参数不是可选的。早在对这些方法进行审核之前,它们可能已使用了隐式解包的可选参数,但不再使用。仔细检查您的方法签名。如果您实际上将类定义为显式符合NSURLConnectionDataDelegate
and NSURLConnectionDelegate
,则应该收到有关此警告。
顺便说一句,您没有使用该NSError
变量,connectionDidFinishLoading
因为在JSONObjectWithData
解析响应时遇到任何问题都可以填充该变量。因此,将该NSError
变量提供给JSONObjectWithData
,您现在就可以诊断在解析过程中发生的任何错误。希望您不会有任何解析错误,但是如果您有解析错误,那么拥有NSError
引用将非常有用。
同样,您不需要它startImmediately
,也不需要false
在主运行循环上对其进行手动调度。仅当您从后台线程启动此连接时,才这样做。但是,由于您是从调用的viewDidLoad
,因此没有必要。
因此它可能最终看起来像:
func startConnection() {
data = NSMutableData()
let url = NSURL(string: formatted)
let request = NSURLRequest(URL: url!)
NSURLConnection(request: request, delegate: self)
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
self.data.appendData(data)
}
func connectionDidFinishLoading(connection: NSURLConnection) {
var error: NSError?
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as? NSDictionary {
println(jsonResult)
} else {
println("parsing error = \(error)")
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString = \(responseString)")
}
}
func connection(connection: NSURLConnection, didFailWithError error: NSError) {
println("connection error = \(error)")
}
顺便说一句,您可能还想实现didReceiveResponse
,因为它还可以提供有关可能会收到的任何服务器错误的信息:
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
if let httpResponse = response as? NSHTTPURLResponse {
let statusCode = httpResponse.statusCode
if statusCode != 200 {
println("expected HTTP response of 200; received \(statusCode)")
println(httpResponse)
}
} else {
println("expected HTTP response; but didn't get any")
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句