未解析的标识符“itemID”

时间:2016-11-16 13:19:12

标签: ios swift

我如何全局定义(扫描条形码),以便我的所有功能都可以访问它。为了说明如何在全球范围内定义“metadataObj”?

class ScanController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

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

override func viewDidLoad() {
super.viewDidLoad()

//Get an instance of the AVCaptureDevice class  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)

    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 = supportedCodeTypes
    // 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!)

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

    //initialize QR Code Frame to highlight the QR Code

    qrCodeFrameView = UIView()
    if let qrCodeFrameView = qrCodeFrameView {
        qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
        qrCodeFrameView.layer.borderWidth = 2
        view.addSubview(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/barcode is detected"
    return
}
//Get metadata object
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

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

    if metadataObj.stringValue != nil {
        messageLabel.text = metadataObj.stringValue
        //Searches firebase for existing barcode
        }
        let itemToSearchFor = metadataObj.stringValue
        let itemID = metadataObj.stringValue
        guard let Description = productDescriptionTextField.text,
        let price = priceTextField.text,
        let location = productLocationTextField.text
        else{
            print("Fill basic product information")
            return
         }
        let ref = FIRDatabase.database().reference(fromURL: " /")
        // creating an  item child node
        let values = ["Item Description": Description, "Image": price, "Location": location, "Price": price ]

        let items = ref.child("Items").child(itemID!)
        items.updateChildValues(values, withCompletionBlock: { (err, ref) in
            if err != nil {
                print(err)
                return
            } })
         FIRDatabase.database().reference().child("Items").child(itemToSearchFor!).observeSingleEvent(of: .value, with:{(snap) in

                print(snap)

                    })

            self.setupNewProductEntry()
           self.setupenterNewProductButton()
}

            }

当我尝试在另一个函数中使用ItemID时出现一个错误。我认为这是因为它不是全局定义的,它只在func captureOutput中定义。关于如何全局定义从条形码扫描仪获得的条形码字符串值的任何想法?

3 个答案:

答案 0 :(得分:0)

您可以将其移到课堂范围之外,这样它就可以通过您的所有模块变得全球化和可见

答案 1 :(得分:0)

在此处(注释所在地)声明它以使其可访问。

class ScanController: UIViewController, AVCaptureMetadataOutputObjectsDelegate {

var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var qrCodeFrameView: UIView?
//DECLARE itemID here
override func viewDidLoad() {
super.viewDidLoad()

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

答案 2 :(得分:0)

只需输入" metadataObj"在全局范围内的类之外(导入部分之后的文件的开头,我的意思是,在类声明之外),因此它不再是该类的成员(并使用您选择的值初始化它) 。如果您在框架中声明它并希望在您的应用中使用它,则添加公开访问修饰符,否则您将无法访问它。 如果您想将它放在类的范围内但可以从代码中的任何位置访问,请添加static修饰符(如果在框架中声明它,则添加 static public )。使用static,您将创建一个变量的单个实例,与您的类的任何特定实例无关(在您的类的所有实例之间共享一个且仅一个metadataObj实例)。如果从不同的线程访问metadataObj,请小心......您可能会面临非常烦人的错误,并且难以修复。在后一种情况下考虑线程同步(不是一个容易应对的主题)......如果你对你需要做什么没有一个非常明确的想法,我会劝阻你。相反,如果你只需要在类范围内(我的意思是,被所有成员函数使用......只有它们),请将它放在Alec告诉你的地方。