After reading a medium sized file (about 500kByte) from a web-service I have a regular Swift String (lines
) originally encoded in .isolatin1
. Befor
If it's ok for you to use a Foundation method on an NSString, I suggest using
enumerateLines(_ block: @escaping (String, UnsafeMutablePointer) -> Void)
Here's an example:
import Foundation
let base = "Hello, playground\r\nhere too\r\nGalahad\r\n"
let ns = base as NSString
ns.enumerateLines { (str, _) in
print(str)
}
It separates the lines properly, taking into account all linefeed types, such as "\r\n", "\n", etc:
Hello, playground
here too
Galahad
In my example I print the lines but it's trivial to count them instead, as you need to - my version is just for the demonstration.