致命错误:在解包可选值(lldb)时意外发现nil
这条消息是因为我的变量设置为nil但代码期望它不是nil . 但我没有解决方案 . 当我从转换和分配中删除问号时,会发生其他错误 .
在 if deviceInput == nil!
处的Thread1致命错误绿色突出显示的行 . 另一个错误绿色突出显示在 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
}
}
}
1 回答
看看你的代码,你应该真的试图摆脱使用
!
强制解包选项的习惯,特别是只是为了“编译” . 一般来说,如果你发现自己写的是if something != nil
,可能会有更好的写一些你想要的东西的方法 . 试着看一下this answer中的例子来复制一些习语 . 你也可以找到this answer对于为什么选项有用的高级解释很有用 .AVCaptureDeviceInput.deviceInputWithDevice
返回一个AnyObject
,你用这一行强行将它转换为AVCaptureInput
:(顺便提一下,你不需要说明
deviceInput
的类型,Swift可以从右边的值推断它)当您编写
as!
时,您告诉编译器“不要与我争辩,强制结果为AVCaptureInput
类型,没有问题” . 如果事实证明返回的是不同类型的东西,那么您的应用程序将崩溃并出现错误 .但是在下一行,你写道:
我实际上对此编译感到非常惊讶!但事实证明它确实如此,并且它崩溃并不奇怪 . 强制展开
nil
的值会崩溃,你正在以最纯粹的形式执行此操作,强制展开nil
literal :)问题是,您已经声明
deviceInput
是非可选类型AVCaptureInput
. 强制转换结果可能不是正确的做法 . 作为国家的文件,处理这个的正确方法是检查结果是
nil
,并采取适当的行动 . 所以你想做的事情如下: