Swift - QR扫描仪抛出零错误

时间:2016-09-10 10:54:40

标签: ios swift

我根据教程here构建了一个QR扫描仪。当应用程序运行时,我可以看到扫描仪,当指向QR码时,屏幕上会显示正确的文本。现在我想要一个按钮(继续),它允许我将扫描的文本值(usernameScanned)转换到下一个视图控制器这不起作用,当我按下按钮时,应用程序崩溃时出现此错误:

  

致命错误:在解包可选值时意外发现nil

这是我将文本存储到usernameScanned变量中的代码:

<record model="ir.ui.view" id="new_id">
            <field name="name">New Wizard</field>
            <field name="model">my.wizard</field>
            <field name="arch" type="xml">
                <form string="New Form"> 
                     <header>
                        <button name="do_generate" string="My Function" type="object"/>
                        or 
                        <button string="Cancel" class ="oe_link" special="cancel"/>
                    </header>
                </form>
            </field>
        </record>

        <record id="action_my_function_wizard" model="ir.actions.act_window">
            <field name="name">My Function</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">my.wizard</field>

            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="target">new</field>

        </record>
        <act_window name="My Function"
            res_model="my.wizard"
            src_model="product.master"
            view_mode="form"
            target="new"
            multi="True"
            key2="client_action_multi"
            id="action_view_my_new_id"/>

这是按钮继续操作:

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection 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 = CGRectZero
            usernameLabel.text = "No barcode/QR code is detected"
            return
        }

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

        // Here we use filter method to check if the type of metadataObj is supported
        // Instead of hardcoding the AVMetadataObjectTypeQRCode, we check if the type
        // can be found in the array of supported bar codes.
        if supportedBarCodes.contains(metadataObj.type) {
            //        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?.transformedMetadataObjectForMetadataObject(metadataObj)
            qrCodeFrameView?.frame = barCodeObject!.bounds

            if metadataObj.stringValue != nil {
                usernameLabel.text = metadataObj.stringValue
                self.usernameScanned = metadataObj.stringValue!
                print(self.usernameScanned)
            }
        }
    }

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

由于您发布的代码没有抛出错误,请在按下按钮后尝试检查您尝试转换的视图控制器。

您可能有未实例化的变量,例如

var userNameProfile:String!

在你的prepareForSegue方法中,我觉得你想要显示扫描结果(毕竟是详细视图) - 所以你应该在这个方法中将该值从VC1传递给VC2。