Skip to content

Instantly share code, notes, and snippets.

@rsfinn
Created November 21, 2016 17:52
Show Gist options
  • Save rsfinn/984ad704e7935335fada11264690b818 to your computer and use it in GitHub Desktop.
Save rsfinn/984ad704e7935335fada11264690b818 to your computer and use it in GitHub Desktop.
Swift 3 `String` extension to convert text file contents to lines, even if file is DOS format
extension String {
func separatedIntoLines() -> [String] {
var lines: [String] = []
let wholeString = self.startIndex..<self.endIndex
self.enumerateSubstrings(in: wholeString, options: .byLines) {
(substring, range, enclosingRange, stopPointer) in
if let line = substring {
lines.append(line)
}
}
return lines
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment