-4

I have this code:

func barcodeData(_ barcode: String!, type: Int32) {
    referenceField.text = barcode!
}

When I scan a barcode the textfield is replaced by it. It's always an "R" in the beginning of the barcode followed by some digits.

I want the textfield to display only the numbers. So if the barcode scanned is "R454512" I want the textfield to show only "454512"

Could someone explain this to me? I'm pretty new in this barcode-world. I don't have any values yet so how am I supposed to use this?

Thanks

pkamb
  • 33,281
  • 23
  • 160
  • 191
moseby
  • 21
  • 2
  • 7
  • What do you mean by **I don't have any values yet** don't you get `R454512` value in `barcode` argument. – Nirav D Jun 09 '17 at 11:11
  • I have the function that uses a external device to scan a barcode. But I don't know the barcode until it's scanned? So how am I supposed to type let text = "R454512"? If there are other values too? – moseby Jun 09 '17 at 11:14
  • @moseby Write like this `referenceField.text = String(barcode.characters.dropFirst())` – Nirav D Jun 09 '17 at 11:18
  • Thank you very much @NiravD – moseby Jun 09 '17 at 11:21

1 Answers1

1

Another way to do this is with a characterSet. This way if there are more letters or spaces at the start or end of the string, you will get the correct result:

barcode.trimmingCharacters(in: CharacterSet.decimalDigits.inverted)
totiDev
  • 5,189
  • 3
  • 30
  • 30