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


Marco S Hyman
 

do {
var str = try String(contentsOf: urlContext.url) // (1)
str.removeAll { $0 == “\r” } // (2)
let lines = str.split(separator: “\n”) // (3)
Oh. Yeah. That’s too much work.


var str = try String(contentsOf: urlContext.url) // (1)
let lines = str.split(separator: “\r\n”) // (3)

works and is faster in that you’ve removed an O(n) operation,

I created your test file with crlf end-of-line (thank you bbedit) and tried it using this code in a playground:

let url = URL(fileURLWithPath: “/path/to.foo.txt”)
if let str = try? String(contentsOf: url) {
let lines = str.split(separator: "\r\n") // (3)
print(lines)
}

Marc

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