Ios 从swift 4 breaks应用程序更新

Ios 从swift 4 breaks应用程序更新,ios,swift,qr-code,Ios,Swift,Qr Code,我和我的朋友最近从swift 3.2升级到了swift 4。不幸的是,对于QR阅读器功能,我们的项目不再正常工作。视频输出工作正常,因此当我们加载页面时,屏幕会在手机观看时显示。但是,它将不再将尺码盒放在屏幕上或识别其前面的二维码 import UIKit import AVFoundation class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

我和我的朋友最近从swift 3.2升级到了swift 4。不幸的是,对于QR阅读器功能,我们的项目不再正常工作。视频输出工作正常,因此当我们加载页面时,屏幕会在手机观看时显示。但是,它将不再将尺码盒放在屏幕上或识别其前面的二维码

    import UIKit
    import AVFoundation
    class ScanViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

    @IBOutlet var messageLabel:UILabel!
    @IBOutlet var topbar: UIView!

    var captureSession: AVCaptureSession?
    var videoPreviewLayer: AVCaptureVideoPreviewLayer?
    var qrCodeFrameView: UIView?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Get an instance of the AVCaptureDevice class to initialize a device object and provide the video as the media type parameter.
        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

        do {
            // Get an instance of the AVCaptureDeviceInput class using the previous device object.
            let input = try AVCaptureDeviceInput(device: captureDevice)

            // Initialize the captureSession object.
            captureSession = AVCaptureSession()

            // Set the input device on the capture session.
            captureSession?.addInput(input)

            // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
            let captureMetadataOutput = AVCaptureMetadataOutput()
            captureSession?.addOutput(captureMetadataOutput)

            // Set delegate and use the default dispatch queue to execute the call back
            captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            captureMetadataOutput.metadataObjectTypes = [AVMetadataObjectTypeQRCode]

            // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
            videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
            videoPreviewLayer?.frame = view.layer.bounds
            view.layer.addSublayer(videoPreviewLayer!)

            // Move the message label and top bar to the front
            view.bringSubview(toFront: messageLabel)
            view.bringSubview(toFront: topbar)

            // Start video capture.
            captureSession?.startRunning()

            // Initialize QR Code Frame to highlight the QR code
            qrCodeFrameView = UIView()

            if let qrCodeFrameView = qrCodeFrameView {
                qrCodeFrameView.layer.borderColor = UIColor.randomColor().cgColor
                qrCodeFrameView.layer.borderWidth = 4
                view.addSubview(qrCodeFrameView)
                view.bringSubview(toFront: qrCodeFrameView)
            }

        } catch {
            // If any error occurs, simply print it out and don't continue any more.
            print(error)
            return
        }
    }

    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {

        // Check if the metadataObjects array is not nil and it contains at least one object.
        if metadataObjects == nil || metadataObjects.count == 0 {
            qrCodeFrameView?.frame = CGRect.zero
            messageLabel.text = "No QR code is detected"
            return
        }

        // Get the metadata object.
        let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

        if metadataObj.type == AVMetadataObjectTypeQRCode {
            // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
            let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
            qrCodeFrameView?.frame = barCodeObject!.bounds

            if metadataObj.stringValue != nil { //Output
                messageLabel.text = metadataObj.stringValue

                performSegue(withIdentifier: "QRFound", sender: self)
            }
        }
    }

    @IBAction func cancel(_ sender: UIButton) {
        if let owningNavController = navigationController {
            owningNavController.popViewController(animated: true)
        }
        dismiss(animated: true, completion: nil)
    }
}

您的委托函数不正确,因此从未调用它。正确的答案是:

optional func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection)
看。 这似乎在Swift 4中有所改变


编辑:另请参阅。

如果您能提供更多关于您在屏幕和调试器中尝试和看到的情况的详细信息,我将提供帮助。