Re: String handling not working — am I going nuts?


Rick Aurbach
 

Ok, I still don’t understand what’s going on, but I’ve got an alternative solution. (Maybe somebody can clarify this for me??)

WHAT DOESN’T WORK IN MY CONTEXT:

do {
    var str = try String(contentsOf: urlContext.url)
    str.removeAll { $0 == “\r” }
    let lines = str.split(separator: “\n”)
   …
} catch {}

BUT THE FOLLOWING WORKS NICELY::

var lines = [String]()
do {
    let str = try String(contentsOf: urlContext.url)
    str.enumerateLines { (line, stop) in
        lines.append(line)
        stop = false
    }
} catch {}

Note: since enumerateLines is not labeled ’throws’, Xcode complains if I try to throw from it’s closure. So instead of working one line at a time within the closure, I need to build a line array and then process it (one line at a time) in a second do-block.

So I have a solution, but I don’t understand the original code didn’t work. Any insights?

Cheers,

Rick Aurbach

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