Search code examples
error-handlingexceptionswift2avcapturedevice

In Swift 2.1, how do you get the NSError that was thrown?


I'm initializing an AVCaptureDeviceInput. In Swift 2 rather than getting an NSError variable you catch an error with a do-try-catch sandwich. However I don't see how to get the NSError inside the catch part. The recommendation given on Apple's developer forum does not indicate how you can access the NSError.

Can someone enlighten me?


Solution

  • This worked for me:

     var error: NSError?
    
            do {
                request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(jsonString, options: [])
            } catch let error1 as NSError {
                error = error1
                request.HTTPBody = nil
            }
    

    Hope this will be helpful