I have a class to handle movie recording with AVFoundation. The initializer will throw an error if any part of the setup fails. The failure error:
"All stored properties of a class instance must be initialized before throwing from an initialize"
This occurs when trying to create a variable from an initializer that also throws an error if initialization fails.
let captureInputDevice = try AVCaptureDeviceInput(device: device)
Code:
enum MovieRecorderError : ErrorType {
case CouldNotInitializeCamera
}
class MovieRecorder: NSObject {
init(previewLayer: UIView) throws {
// Scan through all available AV capture inputs
for device in AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice] {
if device.position == .Back {
do {
let captureInputDevice = try AVCaptureDeviceInput(device: device)
} catch {
throw MovieRecorderError.CouldNotInitializeCamera
}
}
}
}
}
Question
Is this problem caused by instantiating a variable that throws an error, inside a function that throws an error?