I'm confused by what's happening here. This should be simple. I'm checking for the first index of a newline character, but it's returning nil. I've inspected the string in the debugger and it has \r\n
at the end of the string.
rfidstring = {String} "900083000020758\r\n"
if rfidstring.firstIndex(of: "\n") != nil {
print("rfid string has \\n")
}
Am I making some kind of silly mistake and just totally missing it here? When I call contains("\n") that works correctly and returns true. So it knows there is a \n
in the string when I'm calling contains()
but doesn't think there is one when I call firstIndex(of:)
?
// but ...
rfidstring.contains("\n") // returns true
I'm receiving data from an inputstream as I read from an RFID reader, and the reader sends one rfid per line. So to make sure that I've read the entire rfid (sometimes it'll send it in two blocks of data) I need to read to the end of the line. That's why I'm trying to find the newline.
UPDATE: This seems weird to me:
rfidstring.firstIndex(of: "\r\n") // returns valid index: 15[utf8]
rfidstring.firstIndex(of: "\r") // returns nil
rfidstring.firstIndex(of: "\n") // returns nil