ios - 调用 viewdidload 图像中的 Swift 2 额外参数错误

标签 ios swift2 xcode7

我正在使用 Xcode 7 将我的应用程序更新到 Swift 2。这是我的 ViewController viewDidLoad 代码。

 override func viewDidLoad() {
        super.viewDidLoad()

    // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
    // as the media type parameter.
    let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    // Get an instance of the AVCaptureDeviceInput class using the previous device object.
    var error:NSError?

    let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error)

    if (error != nil) {
        // If any error occurs, simply log the description of it and don't continue any more.
        print("\(error?.localizedDescription)")
        return
    }

    // Initialize the captureSession object.
    captureSession = AVCaptureSession()
    // Set the input device on the capture session.
    captureSession?.addInput(input as! AVCaptureInput)

    // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
    let captureMetadataOutput = AVCaptureMetadataOutput()
    captureSession?.addOutput(captureMetadataOutput)

    // Set delegate and use the default dispatch queue to execute the call back
    captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
    captureMetadataOutput.metadataObjectTypes = supportedBarCodes

    // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
    videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
    videoPreviewLayer?.frame = view.layer.bounds
    view.layer.addSublayer(videoPreviewLayer!)

    // Start video capture.
    captureSession?.startRunning()

    // Move the message label to the top view
    view.bringSubviewToFront(messageLabel)

    // Initialize QR Code Frame to highlight the QR code
    qrCodeFrameView = UIView()
    qrCodeFrameView?.layer.borderColor = UIColor.greenColor().CGColor
    qrCodeFrameView?.layer.borderWidth = 2
    view.addSubview(qrCodeFrameView!)
    view.bringSubviewToFront(qrCodeFrameView!)
}

在线

let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error)

我收到错误 Extra argument error in call。我已经尝试使用方法 do{} 和 catch{} 但它不起作用,我总是会收到该错误。

我该如何解决?谢谢

最佳答案

Swift 2 引入了新的错误处理。要解决您遇到的问题,您需要 catch 错误而不是将 NSError 对象传递给 AVCaptureDevice 方法:

override func viewDidLoad() {
    super.viewDidLoad()

    do {
        let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
        let input = try AVCaptureDeviceInput(device: captureDevice)
        // Do the rest of your work...
    } catch let error as NSError {
        // Handle any errors
        print(error)
    }
}

有关更深入的解释,请查看这篇文章:

Error Handling in Swift 2.0

关于ios - 调用 viewdidload 图像中的 Swift 2 额外参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31122985/

相关文章:

ios - 距另一个日期一年的日期

swift - 更改 print(Object) 在 Swift 2.0 中显示的内容

swift - NSFetchRequest - 实体 - 解包可选值时意外发现 nil

c - Xcode 中的访问错误

ios - Xcode 中的文件不工作(名称为红色)

iphone - Apple Core Data 教程中的奇怪错误

ios - UIScrollView 以编程方式滚动

ios - 应用程序组和 UIImage Swift

iOS - 根据百分比用多种颜色填充bezierPath

ios - 使用未解析的标识符 'FIRApp'