0

我在do/catch块中分配一个变量,然后尝试在我的文件中进一步引用该变量。但是当我这样做时,我在 Xcode 中收到以下错误:

使用未解析的标识符“captureDeviceInput”

这是我的代码:

do {
    let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice) as AVCaptureDeviceInput
} catch let error {
    print("\(error)")
    return
}

captureSession = AVCaptureSession()

captureSession?.addInput(input: captureDeviceInput as AVCaptureDeviceInput)

Xcode 似乎无法识别该captureDeviceInput变量。我能做些什么来解决这个问题?

4

1 回答 1

0

captureDeviceInput在本地声明,这意味着它仅在do范围内可见。

将所有好的代码也放在do范围内是一个好习惯。

do {
    let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice) as AVCaptureDeviceInput
    captureSession = AVCaptureSession()
    captureSession?.addInput(input: captureDeviceInput as AVCaptureDeviceInput)
} catch {
    print("\(error)")
    return
}
于 2017-06-25T20:09:11.793 回答