Alex Alex - 2 months ago 17
iOS Question

Swift3: I would like to function read and count QR code

I'm beginner programmer.
Now i build QR code reader App.
I would like to function read and count QR code
now i codeing this function.
i can display count for dynamic button title
However, the QR code value is not counted like 1,2,3.
display natural QR value.
How can I fix it?
sorry my bad english


ViewController


class LentScanViewController: QRScannerViewController {

var device_name = ""
var countNum = 0

@IBOutlet weak var LentButton: UIButton!
@IBAction func lentButton(_ sender: Any) {
performSegue(withIdentifier: "toLentTable",sender: device_name)
}
override func viewDidLoad() {

super.viewDidLoad()
}

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


if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
return
}

let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

if metadataObj.type == AVMetadataObjectTypeQRCode {


let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds

if metadataObj.stringValue != nil {
device_name = metadataObj.stringValue

countNum = countNum + 1
LentButton.setTitle(String(countNum) + "Lent", for:[])
}

}
}

Answer Source

Please try :

var qrCodes:[String] = [] // declare it as member variable in the class

Then call

if metadataObj.stringValue != nil {
    let device_name = metadataObj.stringValue
    if !qrCodes.contains(device_name!) {
        qrCodes.append(device_name!)
    }
    countNum = countNum + 1
    messageLabel.text = String(countNum) + "Lent"
}