-1

I received the error stated above and have tried to amend this by adding in a do / catch block. For some reason the error won't go away. Does anyone know why this might be ?

 override func viewDidAppear(animated: Bool) {
    super.viewWillAppear(animated)

    captureSession = AVCaptureSession()
    captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080

    let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    do {

        let input = AVCaptureDeviceInput(device: backCamera)

        captureSession?.addInput(input)

        stillImageOutput = AVCaptureStillImageOutput()
        stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG]

        if (captureSession?.canAddOutput(stillImageOutput) != nil){
            captureSession?.addOutput(stillImageOutput)

            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
            previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
            oview.layer.addSublayer(previewLayer!)
            captureSession?.startRunning()

        }

    } catch {

    }

}
Jamie Baker
  • 157
  • 6

2 Answers2

0

The clue is in the error description: Error: 'Call can throw but is not marked with try and error not handled'

You have not marked the call that can throw with a try

I don't which call you have in there that throws, but find the one that does and put try in front of it. If you are assigning a value, try needs to go on the right side of the =

EDIT

Just looked in the docs, looks like it's your

let input = AVCaptureDeviceInput(device: backCamera)

statement that can throw. Put a try after the = like this

let input = try AVCaptureDeviceInput(device: backCamera)

Then you can print(error) inside of your catch to see any potentiel errors

Chris
  • 7,830
  • 6
  • 38
  • 72
0

You are writing Swift code. Not Java or C++ code. Exceptions work differently. You need to use try for the single call that can throw, not for a large block of code.

I recommend you download the free Swift 2 book and learn how exceptions work in Swift 2. Similarity with other languages is just superficial.

gnasher729
  • 51,477
  • 5
  • 75
  • 98