Perhaps this will save someone else from deciding that they've lost their mind.
I receive String data from hardware. I get several "lines" of data at a time, separated by carriage-return characters (0x0d) -> "\r".
I split the data into "words" with code like:
let splitChars = "\r " // carriage-return and space
words = incoming.split() { splitChars.contains($0) }
Then I encoutered hardware that was sending carriage-return and new-line to end each line (0x0d0a) -> "\r\n" which of course my code didn't handle. So I change it to
let splitChars = "\r \n"
and it didn't work. I tried lots of combinations. Finally I settled on
let splitChars = "\r\n "
and that did work... unless they weren't sendng the new-lines... then I was broke again.
So here's the facts:
In a String, "\r\n" is ONE character.
It matches only the "character" that is "\r\n".
It does NOT match "\r" or "\n". If you want to match both types of input, you need:
let splitChars = "\r\n \r"
If you need to match new-line by itself also, you can do: let splitChars = "\r\n \n\r"
because "\n\r" is TWO characters, not one.
My sanity is restored (sorta).
Barry