使用未解析的标识符

时间:2016-10-22 16:46:59

标签: swift xcode

输入已定义为try AVCaptureDeviceInput(device: captureDevice),但仍然表示input是未解析的标识符。 请参阅下面的代码,我尝试了多种方法,但没有成功。

import UIKit
import AVFoundation

class ViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

    var captureSession: AVCaptureSession?
    var videoPreviewLayer: AVCaptureVideoPreviewLayer?
    var qrCodeFrameView: UIView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Get an instance of AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.

        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

        // Get an instance of the AVCaptureDeviceInput class using the previous device object.
        do {
            let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
            let input = try AVCaptureDeviceInput(device: captureDevice)
            // Do the rest of your work...
        } catch let error as NSError {
            // Handle any errors
            print(error)
        }

        // Initialize the captureSession object
        captureSession = AVCaptureSession()
        captureSession?.addInput(input as! AVCaptureInput)

        // Set the input device on the capture session.


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

        // Set delegate and use the default dispatch queue to execute the call back 
        captureMetadataOuput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        captureMetadataOuput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]

        // 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()



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

正如其他答案中所述,您的* master remotes/origin/HEAD -> origin/master remotes/origin/master 变量仅限于input块的范围。

另一种解决方案 - 如果你想保留do / catch块 更小和本地化 - 是在块之外声明变量:

do

请注意,这需要您在错误中立即返回 因为 let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) let input: AVCaptureDeviceInput do { input = try AVCaptureDeviceInput(device: captureDevice) } catch let error as NSError { print(error) return // Must return from method here ... } // `input` is defined and initialized now ... captureSession = AVCaptureSession() captureSession?.addInput(input) // ... 将是未定义的。

或者,如果错误消息不重要,请在input中使用try? 语句:

guard

答案 1 :(得分:0)

这是一个范围问题。您的captureDeviceinput常量只能在do块中使用。将您的代码更新为以下内容:

override func viewDidLoad() {
    super.viewDidLoad()
    // Get an instance of AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.

    let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

    // Get an instance of the AVCaptureDeviceInput class using the previous device object.
    do {
        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        let input = try AVCaptureDeviceInput(device: captureDevice)

        // Initialize the captureSession object
        captureSession = AVCaptureSession()
        captureSession?.addInput(input as! AVCaptureInput)

        // Set the input device on the capture session.


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

        // Set delegate and use the default dispatch queue to execute the call back 
        captureMetadataOuput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
        captureMetadataOuput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]

        // 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()
    } catch let error as NSError {
        // Handle any errors
        print(error)
    }
}

答案 2 :(得分:0)

input位于do块的范围内,在外面不可见。

基本上,打印错误并继续,好像什么也没发生是一个非常糟糕的主意。始终将整个 好的代码放入do块:

do {
  let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
  let input = try AVCaptureDeviceInput(device: captureDevice)
  // Initialize the captureSession object
  captureSession = AVCaptureSession()
  captureSession?.addInput(input as! AVCaptureInput)

  // Initialize a AVCaptureMetadataOutput object and set it as the output 

 ...

  // Start video capture
  captureSession?.startRunning()

  // Do the rest of your work...
} catch let error as NSError {
  // Handle any errors
  print(error)
}