Swift 1.2:致命错误:在展开Optional值时意外发现nil

时间:2016-02-08 06:13:04

标签: ios swift

我只想点击图片并在UIImageView上显示。

我已经定义了所有需要的插座和操作,这是代码。

但它显示错误。

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var camera: UIButton!

    @IBOutlet weak var gallery: UIButton!

    @IBOutlet weak var viewImage: UIImageView!

    @IBAction func PhotoGalleryAction(sender: UIButton) {

        let picker = UIImagePickerController()

        picker.delegate = self

        picker.sourceType = .PhotoLibrary

        presentViewController(picker, animated: true, completion: nil)
    }



    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

        viewImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage

        dismissViewControllerAnimated(true, completion: nil)

    }
}

这是错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

1 个答案:

答案 0 :(得分:1)

您必须检查故事板和代码之间的连接。 即使您已连接它,它也可能因使用旧故事板文件或其他内容而损坏(使用Assitant Editor时经常会发生这种情况)