String handling not working — am I going nuts?


Rick Aurbach
 

Can you take a look at this for me? I think I’ve been cooped up at home too long, because whatever is going on, I’m missing it.
 
Context:
I’m working on a data import function for a new app. It looks like I’ve set things up correctly, since if I drop the file (from my desktop to the Simulator), the sceneDelegate’s scene(_:openURLContexts:) method is called. I set up an operations queue to handle the input in background and instantiate the Operation subclass, passing it a URLContext.
 
In the Operation subclass’s main() method, I do the following:
 
do {
var str = try String(contentsOf: urlContext.url) // (1)
str.removeAll {  $0 == “\r” } // (2)
let lines = str.split(separator: “\n”) // (3)
 
The file is UTF8, with lines terminated in “\r\n”
 
After executing (1), the debugger shows:
 
str String "sugar,NO\r\n1/1/20,154\r\n1/2/20,141\r\n1/3/20,147\r\n1/4/20,154\r\n1/5/20,172\r\n1/6/20,170\r\n1/7/20,164\r\n1/8/20,201\r\n1/9/20,163\r\n1/10/20,171\r\n1/11/20,209\r\n1/12/20,202\r\n1/13/20,164\r\n1/14/20,232\r\n1/15/20,182\r\n1/16/20,211\r\n1/17/20,175\r\n1/18/20,174\r\n1/19/20,147\r\n1/20/20,148\r\n1/21/20,150\r\n1/22/20,141\r\n1/23/20,165\r\n1/24/20,169\r\n1/25/20,260\r\n1/26/20,168\r\n1/27/20,179\r\n1/28/20,160\r\n1/29/20,155\r\n1/30/20,181\r\n1/31/20,156\r\n2/1/20,178\r\n2/2/20,202\r\n2/3/20,188\r\n2/4/20,175\r\n2/5/20,157\r\n2/6/20,166\r\n2/7/20,165\r\n2/8/20,146\r\n2/9/20,192\r\n2/10/20,178\r\n2/11/20,172\r\n2/12/20,180\r\n2/13/20,140”
 
which is what I expect.
 
After executing (2), the string is UNCHANGED. (i.e., the “\r” characters were not removed.
 
After executing (3), the ‘lines’ variable contains 1 item which is the whole string (with both the “\r” and “\n” characters present.
 
I’m going nuts here. What’s going on??

Cheers,

Rick Aurbach

Join cocoa@apple-dev.groups.io to automatically receive all group messages.