-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

4
  • What do you mean by I don't have any values yet don't you get R454512 value in barcode argument. Commented Jun 9, 2017 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? Commented Jun 9, 2017 at 11:14
  • @moseby Write like this referenceField.text = String(barcode.characters.dropFirst()) Commented Jun 9, 2017 at 11:18
  • Thank you very much @NiravD Commented Jun 9, 2017 at 11:21

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.