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

mar*_*tus 8 ios xcode7 swift2

我正在使用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!)
}
Run Code Online (Sandbox Code Playgroud)

在线

let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error)
Run Code Online (Sandbox Code Playgroud)

我在调用中得到错误额外参数错误.我已经尝试使用方法do {}并捕获{}但它没有用,我总是得到那个错误.

我该如何解决这个问题?谢谢

sba*_*row 16

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

有关更深入的解释,请查看本文:

Swift 2.0中的错误处理


fqd*_*qdn 0

看起来该类型方法不再存在AVCaptureDeviceInput,请参阅 -> https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVCaptureDeviceInput_Class/index.html#//apple_ref/swift/ cl/c:objc(cs)AVCaptureDeviceInput

(看起来你可能想使用init(device:)

...作为一个方便的提示:每当您通过网络浏览开发人员库时,如果您不确定是否看到最新的“预发布”版本的文档,请检查 URL -> 添加“/prerelease”如有必要,在“library”和“/ios”之间:)