下载数据(JSON)无法正常工作:在解包Optional值时意外发现nil

时间:2016-04-01 19:29:28

标签: ios json swift

我正在制作一个“我的天气应用程序”,我正在练习从网上下载数据。但是在使用静态URL进行测试时出现错误,我不知道为什么。

我的天气模型的一部分:

init(location: String, temp: Double, icon: String) {
        self._location = location
        self._temp = temp
        self._icon = icon

        url = "http://api.openweathermap.org/data/2.5/forecast?lat=52.379189&lon=4.899431&APPID=28c704926d4f9131886a0bce5d2425d9"
    }

    func downloadWeatherDetails(completed: DownloadComplete) {
        let weatherUrl = NSURL(string: url)!
        Alamofire.request(.GET, weatherUrl).responseJSON { response in
            let result = response.result
            print(result.value.debugDescription)
        }
    }

我的ViewController的一部分:

var weather: Weather!

    override func viewDidLoad() {
        super.viewDidLoad()

        weather.downloadWeatherDetails() { () -> () in
            //This will be called after download is done
       }
    }

我的常数'文件:

typealias DownloadComplete = () -> ()

当我在浏览器中测试网址时,网址正在运行,因此不重要。我不知道出了什么问题。有谁可以帮助我?

非常感谢!

1 个答案:

答案 0 :(得分:1)

您需要Weather类的实例。

var weather = Weather()