2

I am following this tutorial to make a camera app, but the tutorial is in Swift and I am using Xcode 7 beta and Swift2.

http://jamesonquave.com/blog/taking-control-of-the-iphone-camera-in-ios-8-with-swift-part-1/

I heard that Swift2 has a different error-handling method now, so I guess this part of code needs to be changed. But I am not sure how. Can anyone help me with this? Thanks!

var err : NSError? = nil
        captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))

        if err != nil {
            println("error: \(err?.localizedDescription)")
        }
user3768495
  • 4,077
  • 7
  • 32
  • 58
  • You can find the answer to this question https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID512 – Kevin Jul 12 '15 at 18:00
  • I'm struggling to infer the answer from the link you provided @Kevin . Would you be able to provide an explicit answer for the given example? thanks – Ian Sep 17 '15 at 15:06

2 Answers2

9

In order to get the code you posted to match the new error handling requirements in swift 2 you need to replace your lines of code with the following:

var err : NSError? = nil
do {
    let input = try AVCaptureDeviceInput(device: captureDevice)
    captureSession.addInput(input)
} catch _ {
    print("error: \(err?.localizedDescription)")
}

The reason for this change is that Apple has changed the way you need to handle errors in order to make the coding less error prone. Now you need to have a do catch statement for any call that is going to throw an error. The call that could throw and error needs to be in the do statement and the error handling needs to be found in the catch statement. There a bit more detail as to the reason for this change in the following link:

https://www.hackingwithswift.com/new-syntax-swift-2-error-handling-try-catch

Chris
  • 91
  • 1
  • 5
  • 1
    Can you make it more obvious to the asker what the actual answer is and why that answer works? – Rob Norback Sep 30 '15 at 20:51
  • Definitely, being new to the site should I edit the existing answer or do the revision as a comment? Thanks for the advice! – Chris Oct 01 '15 at 15:52
  • Just edit the existing answer and you should be good. Glad to have you contributing to the site! – Rob Norback Oct 01 '15 at 15:56
  • Rob, this is an incredible resource for the community. I'm happy to give back! Let me know if my edit is more direct and offers enough explanation why. Thanks! – Chris Oct 05 '15 at 15:55
1

Explanation of the new Swift error handling - Error-Handling in Swift-Language

let captureSession = AVCaptureSession()
let captureDevice: AVCaptureDevice
do {
    let input = try AVCaptureDeviceInput(device: captureDevice)
    captureSession.addInput(input)
}
Community
  • 1
  • 1
Kevin
  • 16,696
  • 7
  • 51
  • 68
  • 5
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – JAL Sep 17 '15 at 20:00