ios - 线程 1 : exc_bad_instruction(code=exc_i386_invop, 子代码=0x0)错误

标签 ios swift exception option-type

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


}
@IBAction func onbutton(sender: UIButton) {

    inputarea.text = "Success"
    getweatherdata("http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98")

    // UIApplication.sharedApplication().openURL(NSURL(string:"http://www.reddit.com/")!)
}

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

func getweatherdata(urlString : String){

    let url = NSURL(string: urlString)
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) { (data, response, error) in dispatch_async(dispatch_get_main_queue(),{
        self.setLabels(data!)
        })
    }
    task.resume()
}

我正在尝试实现一个简单的天气应用程序,我能够编译代码,但执行后我在函数调用 self 时遇到错误“thread 1: exc_bad_instruction(code=exc_i386_invop,subcode=0x0) error” .setLabels(data!),我检查了url,它返回的数据不是nil。

我有 setLabels 函数,可以提取子数据并显示它,如果有人需要,我将非常乐意提供完整的代码。谢谢

请找到下面的setLabels函数,appname和templ是View中的标签

        func setLabels(weatherdata: NSData){
        do {
            if let jsonResult = try NSJSONSerialization.JSONObjectWithData(weatherdata, options: []) as? NSDictionary {
                print(jsonResult)

                if let name = jsonResult["name"] as? String{
                    appname.text = name
                }

                if let main = jsonResult["main"] as? String{
                    //if let temp = main["temp"] as? Double {
                    //templ.text = String(format: "%.1f",temp)
                    templ.text = main

                }
            }
        } catch let error as NSError {
            print(error.localizedDescription)
        }

最佳答案

可能是您未正确解包 JSON 数据。如果输入错误,打开包装时可能会发现 nil。您是否使用 if let 来解析您的 JSON,或者某种 JSON 解析器?您介意发布完整的 setLabels() 函数吗?

关于ios - 线程 1 : exc_bad_instruction(code=exc_i386_invop, 子代码=0x0)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35189862/

相关文章:

exception - 为什么使用nodejs上传异常

ios - 如何更改 Shake/Shake Gesture 的键盘快捷键?

iOS UITableView reloadData 仅更新我添加的最后一个条目,如何重新加载所有内容?

swift - 为什么我的 gestureRecognizer 返回零?

arrays - 将多个值附加到结构中的数组?

.net - .Net : try-catch block without any Exception as parameter for catch? 是什么意思

ios - 找不到 'AVCapturePhotoCaptureDelegate' 的协议(protocol)声明

ios - [LAContext biometryType] : unrecognized selector on iOS 11. 0.0

ios - 已经显式地将 NSArray 转换为 [AnyObject] 但仍然收到错误

multithreading - 如何在没有主线程等待的情况下从长时间运行的后台任务引发异常