1
votes

I have the following code :

func startCameraFromViewController(viewController: UIViewController, withDelegate delegate:
protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>) -> Bool {

    if (UIImagePickerController.isSourceTypeAvailable(.Camera) == false) {
        return false
    }

    let cameraController = UIImagePickerController()
    cameraController.sourceType = .Camera
    cameraController.mediaTypes = [kUTTypeMovie as String]
    cameraController.allowsEditing = false
    cameraController.delegate = delegate

    presentViewController(cameraController, animated: true, completion: nil)
    return true    
}

It is being called as follows :

func recordVideoButtonTapped() {
    startCameraFromViewController(self, withDelegate: self)
}

But for some reason, I am getting the following error

fatal error: unexpectedly found nil while unwrapping an Optional value

on this line :

 presentViewController(cameraController, animated: true, completion: nil)

Please help debug.

This question is different from

What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

which is a more general question relating to unwrapping of optionals

This question pertains specifically to UIImagePickerController

1
It's not a duplicate. The context in which this error was encountered is different.Arunabh Das

1 Answers

0
votes

I'm using Xcode 6.4 and it works fine:

import UIKit
import MobileCoreServices

class FirstViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func startCameraFromViewController(viewController: UIViewController, withDelegate delegate:
        protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>) -> Void {

            if (UIImagePickerController.isSourceTypeAvailable(.Camera) == false) {
                println("fail")
                //return false
            }

            let cameraController = UIImagePickerController()
            cameraController.sourceType = .Camera
            cameraController.mediaTypes = [kUTTypeMovie as String]
            cameraController.allowsEditing = false
            cameraController.delegate = delegate

            presentViewController(cameraController, animated: true, completion: nil)
            println("ok")
            //return true
    }

    @IBAction func ToSecondPressed(sender: AnyObject) {
        startCameraFromViewController(self, withDelegate: self)  
    }
}