Ios Xcode 8 swift 3从URL加载图像并将其传递到序列目标

Ios Xcode 8 swift 3从URL加载图像并将其传递到序列目标,ios,swift,xcode,Ios,Swift,Xcode,当我运行这段代码时,我得到一条EXC_BAD_指令,我不知道为什么: override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller.

当我运行这段代码时,我得到一条EXC_BAD_指令,我不知道为什么:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
        if (segue.identifier == "tblancSegue"){
            let viewVC = segue.destination as! DetailCoursViewController
            //viewVC.delegate = self
            viewVC.nomDuCours =  self.cours1Lbl.text
            do {
                let myImage = try UIImage(data: NSData(contentsOf: NSURL(string:self.cours1Image!) as! URL) as Data)
                viewVC.imageCours.image = myImage
            } catch {
                print("foirage")
            }
        }

    }

}
是let myImage使应用程序崩溃


感谢您抽出时间……

问题似乎出在以下几行:

viewVC.imageCours.image = myImage

如果
imageCours
是UIImageView,则该视图可能尚未创建。您应该只传递图像,并在目标ViewController的
viewDidLoad()
中设置它。

记住,
nil
,则code>运算符表示“在此崩溃”。通常该错误表示您得到了
nil
。通过查看您的代码,我觉得您可能实际上没有获得图像。您可以尝试打印获取图像的结果,看看是否得到结果。为什么不必要地使用
NSURL
?只需使用
URL
。将有问题的行拆分为3行,以便正确读取和调试每个值。我假设
imageCours
是一个图像视图。它可能尚未创建。您应该只传递图像,并在目标VC的
viewDidLoad()
中进行设置。