当我尝试从情节提要中继续时,为什么我在这行代码中遇到段错误?

Why am I getting a seg fault in this line of code when I try to segue from storyboard?

因此,我正在尝试将情节提要中的一个按钮连接到另一个 ViewController。但是,每次我点击按钮时,应用程序都会崩溃。这是应用程序崩溃时突出显示的代码行。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let barcodeViewController: BarcodeReaderViewController = segue.destination as! BarcodeReaderViewController
    barcodeViewController.delegate = self

}

这是我的 BarCodeVC

import UIKit
import AVFoundation

protocol BarcodeDelegate {
  func barcodeReaded(barcode: String)
}

class BarcodeViewController: UIViewController, 
                             AVCaptureMetadataOutputObjectsDelegate {

var delegate: BarcodeDelegate?

var videoCaptureDevice: AVCaptureDevice = 
 AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
var device = AVCaptureDevice.defaultDevice(withMediaType: 
  AVMediaTypeVideo)
var output = AVCaptureMetadataOutput()
var previewLayer: AVCaptureVideoPreviewLayer?

var captureSession = AVCaptureSession()
var code: String?

override func viewDidLoad() {
  super.viewDidLoad()

  self.view.backgroundColor = UIColor.clear
  self.setupCamera()
}

private func setupCamera() {

  let input = try? AVCaptureDeviceInput(device: videoCaptureDevice)

  if self.captureSession.canAddInput(input) {
      self.captureSession.addInput(input)
  }

  self.previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

  if let videoPreviewLayer = self.previewLayer {
      videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
      videoPreviewLayer.frame = self.view.bounds
      view.layer.addSublayer(videoPreviewLayer)
  }

  let metadataOutput = AVCaptureMetadataOutput()
  if self.captureSession.canAddOutput(metadataOutput) {
      self.captureSession.addOutput(metadataOutput)

      metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
      metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code]
  } else {
      print("Could not add metadata output")
  }
}

 override func viewWillAppear(_ animated: Bool) {
   super.viewWillAppear(animated)

   if (captureSession.isRunning == false) {
      captureSession.startRunning();
   }
}

override func viewWillDisappear(_ animated: Bool) {
  super.viewWillDisappear(animated)

  if (captureSession.isRunning == true) {
     captureSession.stopRunning();
  }
}

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
   // This is the delegate'smethod that is called when a code is readed
   for metadata in metadataObjects {
       let readableObject = metadata as! AVMetadataMachineReadableCodeObject
       let code = readableObject.stringValue


       self.dismiss(animated: true, completion: nil)
       self.delegate?.barcodeReaded(barcode: code!)
       print(code!)
   }
 }
}

我的 segue 只是一个简单的拖动来显示,而不是以编程方式。所以我对它崩溃的原因有点困惑。

这是错误:

Could not cast value of type 'BarCodeProj.ViewController' (0x1000361b0) to 'BarCodeProj.BarcodeViewController' (0x100035a78).

在这种情况下,控制台错误会告诉您需要知道的一切。 segue.destination 不是 BarcodeReaderViewController,因此您尝试将其转换为一个(使用 as!)会导致灾难性的失败。

首先,检查你的故事板并确保正确设置视图控制器的类型(你要转换到的视图控制器。)

通过在情节提要中选择适当的视图控制器并在此处选中来执行此操作: