线程1 EXC_bad_instruction(code = exc_1386_invop subcode = 0x0)

时间:2016-10-10 04:32:51

标签: ios swift xcode xcode8

我的代码有问题。为什么它给我一个错误"线程1 EXC_bad_instruction(code = exc_1386_invop subcode = 0x0)"在"让会话"线?

import Foundation

protocol WeatherServiceDelegate{
    func setWeather(weather:Weather)
}

class WeatherService{

    var delegate: WeatherServiceDelegate?
    func getWeather(city: String){
        let path = "http://api.openweathermap.org/data/2.5/weather?q=Boston&"
        let url = URL(string: path)

        let session = URLSession.shared.dataTask(with: url!) { (data: Data?, respone:URLResponse?,error: Error?) in
            print(">>>>>\(data)")
        }

        session.resume()

1 个答案:

答案 0 :(得分:0)

如果您在展开时发现“nil”,则网址创建无效

一般情况下,您应尽可能远离!并使用if letguard let进行投射。在这种情况下,我不确定为什么它会失败,但如果你隔离了网址创建,你可能能够找出问题。