NSURLConnection没有调用方法(第一次工作)

时间:2015-06-08 01:49:43

标签: ios json swift nsurlconnection

所以我使用 plotOptions: { series: { point: { events: { mouseOver: function () { var chart = this.series.chart; if (!chart.lbl) { chart.lbl = chart.renderer.label('') .attr({ padding: 10, r: 10, fill: Highcharts.getOptions().colors[1] }) .css({ color: 'red' }) .add(); } chart.lbl .show() .attr({ text: 'x: ' + this.x + ', y: ' + this.y }); } } }, events: { mouseOut: function () { if (this.chart.lbl) { this.chart.lbl.hide(); } } } } } 从网站获取JSON数据,它可以工作,我将数据存储在搜索控制器/表视图中。现在,当用户点击视图中的单元格时,我想要转换到新视图,并从网站加载另一组JSON数据。但是,在这个新视图中,连接永远不会运行方法。这是我正在做的两次(每个都在不同的视图控制器中)

NSURLConnection

从断点看,它似乎永远不会运行:

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()

1 个答案:

答案 0 :(得分:1)

也实施connection:didFailWithError:。看看它报道了什么。没有它,你无法知道它失败的原因。

func connection(connection: NSURLConnection, didFailWithError error: NSError) {
    println("connection error = \(error)")
}

此外,在didReceiveData(以及其他方法)中,NSURLConnection参数不是可选的。在审核之前,他们可能已经使用了隐式未包装的选项,但不再使用。仔细检查您的方法签名。如果您确实将您的类定义为明确符合NSURLConnectionDataDelegateNSURLConnectionDelegate,那么您应该收到有关此问题的警告。

顺便说一下,如果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")
    }
}