在展开 Optional 值时意外发现 nil?斯威夫特4
Unexpectedly found nil while unwrapping an Optional value? Swift4
我已经 寻找解决方案,并尝试了几种方法,none 都有效。也许我遇到的这个问题可以帮助一些没有从我提供的 link 中得到解决方案的人。
我正在尝试设置相机,但是,当我 运行 应用程序时,我收到以下信息。
Unexpectedly found nil while unwrapping an Optional value
这发生在下面指示的行上:
func setupInputOutput() {
// if currentCamera != nil {
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!) //Here error happens
captureSession.addInput(captureDeviceInput)
photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
} catch {
print("error -- \(error)")
}
// }
}
我声明 currentCamera 如下所示:
var currentCamera: AVCaptureDevice?
在初始化你的 AVCaptureDeviceInput
之前获取默认值 AVCaptureDevice
并将其设置到你的 currentCamera
属性 中,像这样
guard let device: AVCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
for: .video, position: .back) else {
return
}
self.currentCamera = device
您正在崩溃,因为您正在使用 ! 强制展开 currentCamera。当它可能为零时,此时,对于您的注释行,您不确定它是否为零。
首先,你应该确保你的 AVCaptureDevice 已经在到达这一点之前被初始化
currentCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
在 swift 中,通常不应使用 == nil 和 != nil。这正是可选项的用途,swift 有自己的处理方式。
func setupInputOutput() {
if let currentCamera = currentCamera {
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!) //Here error happens
captureSession.addInput(captureDeviceInput)photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
} catch {
print("error -- \(error)")
}
}
}
如果您不熟悉选项和展开,您应该查看有关它的 Apple 文档,可在此处找到:https://developer.apple.com/documentation/swift/optional
var currentCamera: AVCaptureDevice?
当您使用 AVCaptureDevice?
时?意味着它是一个可选的变量。 currentCamera
可以是 AVCaptureDevice
也可以是 nil
3 种处理可选值的方法:
// Using Guard
guard let currentCam = currentCamera else {return}
// do stuff using currentCam
// Using if
if let currentCam = currentCamera {
// do stuff using currentCam
}
// Ugliest
if currentCamera != nil {
// do stuff using currentCam
}
我已经
我正在尝试设置相机,但是,当我 运行 应用程序时,我收到以下信息。
Unexpectedly found nil while unwrapping an Optional value
这发生在下面指示的行上:
func setupInputOutput() {
// if currentCamera != nil {
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!) //Here error happens
captureSession.addInput(captureDeviceInput)
photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
} catch {
print("error -- \(error)")
}
// }
}
我声明 currentCamera 如下所示:
var currentCamera: AVCaptureDevice?
在初始化你的 AVCaptureDeviceInput
之前获取默认值 AVCaptureDevice
并将其设置到你的 currentCamera
属性 中,像这样
guard let device: AVCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera,
for: .video, position: .back) else {
return
}
self.currentCamera = device
您正在崩溃,因为您正在使用 ! 强制展开 currentCamera。当它可能为零时,此时,对于您的注释行,您不确定它是否为零。
首先,你应该确保你的 AVCaptureDevice 已经在到达这一点之前被初始化
currentCamera = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
在 swift 中,通常不应使用 == nil 和 != nil。这正是可选项的用途,swift 有自己的处理方式。
func setupInputOutput() {
if let currentCamera = currentCamera {
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!) //Here error happens
captureSession.addInput(captureDeviceInput)photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
} catch {
print("error -- \(error)")
}
}
}
如果您不熟悉选项和展开,您应该查看有关它的 Apple 文档,可在此处找到:https://developer.apple.com/documentation/swift/optional
var currentCamera: AVCaptureDevice?
当您使用 AVCaptureDevice?
时?意味着它是一个可选的变量。 currentCamera
可以是 AVCaptureDevice
也可以是 nil
3 种处理可选值的方法:
// Using Guard
guard let currentCam = currentCamera else {return}
// do stuff using currentCam
// Using if
if let currentCam = currentCamera {
// do stuff using currentCam
}
// Ugliest
if currentCamera != nil {
// do stuff using currentCam
}