gpt4 book ai didi

ios - fatal error : unexpectedly found nil while unwrapping an Optional value in AVFoundation

转载 作者:行者123 更新时间:2023-11-28 07:15:16 25 4
gpt4 key购买 nike

我正在制作一个使用闪光灯作为手电筒的应用程序。我正在编写以下代码,XCode 在第一行就给我错误。任何人都可以提出它的根本原因吗?我已完成调试,但似乎无法找到它..

@IBAction func powerButton(sender: UIButton)
{
NSLog("flashButtonClicked")

let flashLight : AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)!

torchButton.setBackgroundImage(switchOnImage, forState:UIControlState.Normal)
NSLog("Button Background changed!!")

if flashLight.torchAvailable && flashLight.isTorchModeSupported(AVCaptureTorchMode.On)
{
NSLog("inside if...")
let success = flashLight.lockForConfiguration(nil)

if (success)
{
if flashLight.torchActive
{
flashLight.isTorchModeSupported(AVCaptureTorchMode.Off)
onOffbutton.setBackgroundImage(switchOffImage, forState:UIControlState.Normal)
}
else
{
flashLight.setTorchModeOnWithLevel(100, error: nil)
onOffbutton.setBackgroundImage(switchOnImage, forState:UIControlState.Normal)
}
flashLight.unlockForConfiguration()
}
}
}

最佳答案

AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 返回一个 nil 对象。您正在尝试解包它 (!) 并将其分配给不可为空的常量 let flashLight : AVCaptureDevice

尝试添加 nil 的检查

var flashLight = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
if flashLight != nil {
//rest of your code here
}

关于ios - fatal error : unexpectedly found nil while unwrapping an Optional value in AVFoundation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26592943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com