0

I have a project with various class files. I have a barcode scanner which I have used from an online source which outputs the values in an alert controller. What I would like to do is take the barcode value and pass it back to a my main class and using a function parse it and display it in the relevant labels. For some reason I cannot get it to do this if anybody has any ideas that would be great. I have spent all day trying to figure this out without any luck.

barcodeScanner class relevant section

/* AVCaptureMetadataOutputObjectsDelegate */

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {

    if alertController != nil {
        return
    }

    if metadataObjects != nil && metadataObjects.count > 0 {
        if let machineReadableCode = metadataObjects[0] as? AVMetadataMachineReadableCodeObject {
            // get the barcode string
            let type = machineReadableCode.type
            let barcode = machineReadableCode.stringValue

I need to get the barcode let value above to the passengerInformation class where it will be passed through submitCodeAuto function to write the labels.

            passengerInformation().self.submitCodeAuto(barcode)

My crack at it above which doesn't seem to work..

            // display the barcode in an alert
            let title = "Barcode"
            let message = "Type: \(type)\nBarcode: \(barcode)"
            displayAlert(title, message: message)
        }

    }
}

}

passengerInformation class

@IBOutlet weak var firstNameResponse: UILabel!
@IBOutlet weak var lastNameResponse: UILabel!

@IBAction func submitCodeAuto(sender: AnyObject!) {
   firstNameResponse.text = barcodeProtocol(barcode).firstName
   lastNameResponse.text = barcodeProtocol(barcode).lastName
}

Above the submitCodeAuto function also sends the incoming barcode through another function called barcodeProtocol which formats it allowing for first and last name to be retrieved.

I have currently tested the labels with a button running a textfield value through barcodeProtocol and displaying in the labels so that all is working.

I have also hooked up a button to a new viewcontroller with the scanner class that works fine. Showing the camera scanning and displaying the value.

but I just haven't been able to join them up. The app is returning fatal error: unexpectedly found nil while unwrapping an Optional value

Any help would be great thanks.

A.Denis
  • 39
  • 5
  • 1
    Did you do a search on the error message `fatal error: unexpectedly found nil while unwrapping an Optional value`? Did you look at the problem line indicated in Xcode? What debugging have you attempted? There are lots of similar question on StackOverflow. For example: http://stackoverflow.com/q/32170456/558933 – Robotic Cat May 27 '16 at 19:05

2 Answers2

1

In your function:

@IBAction func submitCodeAuto(sender: AnyObject!) {
   firstNameResponse.text = barcodeProtocol(barcode).firstName
   lastNameResponse.text = barcodeProtocol(barcode).lastName
}

where did you get 'barcode' from?

Try replacing it with

@IBAction func submitCodeAuto(sender: AnyObject!) {
   firstNameResponse.text = barcodeProtocol(sender as! String).firstName
   lastNameResponse.text = barcodeProtocol(sender as! String).lastName
}

I am assuming that your method barcodeProtocol takes in any string and perfectly parses it into firstname and lastname

Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38
-1

Your code is riddled with problems.

You create a new instance of your passengerInformation class from your barcodeScanner code, invoke the submitCodeAuto() method, and then forget about the newly created passengerInformation object. (presumably it's a view controller.)

It does not make sense to create a new view controller, send it a message, and then forget about it. That won't do anything. You probably want to instantiate your custom passengerInformation view controller from a storyboard, set a barcode string property in the view controller, and then present it modally using presentViewController:animated:completion:. In your passengerInformation view controller's viewWillAppear method, you should take the barcode property, extract the info you need from it, and install it into your text fields.

Your submitCodeAuto() method is declared as an IBAction even though you're not using it that way. It takes a parameter sender which you ignore, and instead you use a variable barcode which you don't show.

Your submitCodeAuto() function should probably take a parameter barcode of type String, since that's what it seems to be doing.

You have several classes who's class names start with lower-case letters. Class names should start with upper-case letters.

Duncan C
  • 128,072
  • 22
  • 173
  • 272