致命错误:在展开Optional值(lldb)时意外发现nil
此消息是因为我的变量设置为nil但代码期望它不是nil。但我没有解决方案。当我从转换和分配中删除问号时,会发生其他错误。
线程1
致命错误绿色突出显示if deviceInput == nil!
处的行。和
beginSession()
来电时另一个错误绿色高亮显示的行。
应用程序启动,相机手电筒根据我的代码自动打开但随后应用程序崩溃。在火炬仍然亮着的情况下,应用程序仍停留在启动屏幕上。
你能看一下相机会话的设置并告诉我出了什么问题吗?感谢
import UIKit
import Foundation
import AVFoundation
import CoreMedia
import CoreVideo
class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
let captureSession = AVCaptureSession()
var captureDevice : AVCaptureDevice?
var validFrameCounter: Int = 0
// for sampling from the camera
enum CurrentState {
case statePaused
case stateSampling
}
var currentState = CurrentState.statePaused
override func viewDidLoad() {
super.viewDidLoad()
captureSession.sessionPreset = AVCaptureSessionPresetHigh
let devices = AVCaptureDevice.devices()
// Loop through all the capture devices on this phone
for device in devices {
// Make sure this particular device supports video
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera
if(device.position == AVCaptureDevicePosition.Back) {
captureDevice = device as? AVCaptureDevice
if captureDevice != nil {
//println("Capture device found")
beginSession() // fatal error
}
}
}
}
}
// configure device for camera and focus mode
// start capturing frames
func beginSession() {
// Create the AVCapture Session
var err : NSError? = nil
captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &err))
if err != nil {
println("error: \(err?.localizedDescription)")
}
// Automatic Switch ON torch mode
if captureDevice!.hasTorch {
// lock your device for configuration
captureDevice!.lockForConfiguration(nil)
// check if your torchMode is on or off. If on turns it off otherwise turns it on
captureDevice!.torchMode = captureDevice!.torchActive ? AVCaptureTorchMode.Off : AVCaptureTorchMode.On
// sets the torch intensity to 100%
captureDevice!.setTorchModeOnWithLevel(1.0, error: nil)
// unlock your device
captureDevice!.unlockForConfiguration()
}
// Create a AVCaptureInput with the camera device
var deviceInput : AVCaptureInput = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &err) as! AVCaptureInput
if deviceInput == nil! { // fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
println("error: \(err?.localizedDescription)")
}
// Set the output
var videoOutput : AVCaptureVideoDataOutput = AVCaptureVideoDataOutput()
// create a queue to run the capture on
var captureQueue : dispatch_queue_t = dispatch_queue_create("captureQueue", nil)
// setup ourself up as the capture delegate
videoOutput.setSampleBufferDelegate(self, queue: captureQueue)
// configure the pixel format
videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String : Int(kCVPixelFormatType_32BGRA)]
// kCVPixelBufferPixelFormatTypeKey is a CFString btw.
// set the minimum acceptable frame rate to 10 fps
captureDevice!.activeVideoMinFrameDuration = CMTimeMake(1, 10)
// and the size of the frames we want - we'll use the smallest frame size available
captureSession.sessionPreset = AVCaptureSessionPresetLow
// Add the input and output
captureSession.addInput(deviceInput)
captureSession.addOutput(videoOutput)
// Start the session
captureSession.startRunning()
func setState(state: CurrentState){
switch state
{
case .statePaused:
// what goes here? Something like this?
UIApplication.sharedApplication().idleTimerDisabled = false
case .stateSampling:
// what goes here? Something like this?
UIApplication.sharedApplication().idleTimerDisabled = true
}
}
// sampling from the camera
currentState = CurrentState.stateSampling
// stop the app from sleeping
UIApplication.sharedApplication().idleTimerDisabled = true
// update our UI on a timer every 0.1 seconds
NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
func stopCameraCapture() {
captureSession.stopRunning()
}
// pragma mark Pause and Resume of detection
func pause() {
if currentState == CurrentState.statePaused {
return
}
// switch off the torch
if captureDevice!.isTorchModeSupported(AVCaptureTorchMode.On) {
captureDevice!.lockForConfiguration(nil)
captureDevice!.torchMode = AVCaptureTorchMode.Off
captureDevice!.unlockForConfiguration()
}
currentState = CurrentState.statePaused
// let the application go to sleep if the phone is idle
UIApplication.sharedApplication().idleTimerDisabled = false
}
func resume() {
if currentState != CurrentState.statePaused {
return
}
// switch on the torch
if captureDevice!.isTorchModeSupported(AVCaptureTorchMode.On) {
captureDevice!.lockForConfiguration(nil)
captureDevice!.torchMode = AVCaptureTorchMode.On
captureDevice!.unlockForConfiguration()
}
currentState = CurrentState.stateSampling
// stop the app from sleeping
UIApplication.sharedApplication().idleTimerDisabled = true
}
}
}
答案 0 :(得分:1)
看看你的代码,你应该尝试摆脱使用!
强制解包选项的习惯,尤其是“编译”。一般来说,如果你发现自己写if something != nil
,可能有更好的方式来编写你想要的东西。试着查看this answer中的例子来复制一些习语。你也可能会发现this answer对于高级别有用解释选项有用的原因。
AVCaptureDeviceInput.deviceInputWithDevice
会返回AnyObject
,您可以使用此行将其强制转换为AVCaptureInput
:
var deviceInput = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &err) as! AVCaptureInput
(顺便说一下,你不需要说明deviceInput
的类型,Swift可以从右边的值推断出它)
当您编写as!
时,您告诉编译器“不要与我争论,强制结果为AVCaptureInput
类型,没有问题”。如果事实证明返回的是不同类型的东西,那么您的应用程序将崩溃并出现错误。
然后在下一行,你写道:
if deviceInput == nil! {
我真的对这个编译感到非常惊讶!但事实证明它确实如此,并且它崩溃并不奇怪。强制展开nil
的值将崩溃,并且您正在以最纯粹的形式执行此操作,强制展开nil
文字:)
问题是,您已经声明deviceInput
是非可选类型AVCaptureInput
。强制转换结果可能不是正确的做法。作为状态的文档,
如果设备因为不再可用或因为它正在使用而无法打开,例如,此方法返回
nil
,可选的outError参数指向描述问题的NSError
处理此问题的正确方法是检查结果是nil
,并采取适当的行动。所以你想做类似的事情:
if let deviceInput = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &err) as? AVCaptureInput
// use deviceInput
}
else {
println("error: \(err?.localizedDescription)")
}