println(currentWeatherDictionary)函数不起作用

时间:2014-10-22 23:08:09

标签: ios swift

这是我的代码:println(currentWeatherDictionary)应该从该URL打印出那些坐标的天气,但它什么都没打印出来,这是xcode的问题吗?我正在使用xcode 6.01

import UIKit

class ViewController: UIViewController {

    //Replace the string below with your API Key.
    private let apiKey = "d22b3b492d25282da275e826cc50757b"

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
        let forecastURL = NSURL(string: "43.067827,-77.541085", relativeToURL: baseURL)


        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in

            if (error == nil) {
                let dataObject = NSData(contentsOfURL: location)
                let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary


                let currentWeatherDictionary: NSDictionary = weatherDictionary["currently"] as NSDictionary

                println(currentWeatherDictionary)

            }

        })

        downloadTask.resume()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

1 个答案:

答案 0 :(得分:0)

您的println(currentWeatherDictionary)行无法执行,也无法打印任何内容。确保error不是nil并且正在调用完成处理程序。您可以使用断点或其他println语句来执行此操作。