0

I'm currently trying to take a text file and go through it line by line, creating an array of strings where each element represents a line of the text file. The following is my current code:

let file = String(contentsOfURL: selectedfile!, encoding: NSUTF8StringEncoding, error: nil)!
let FileArray = file.componentsSeparatedByString("\r\n")

Unfortunately when I run the program, instead of an array of 50 elements or so, I get an array of '106652628048000' elements, where the first one is the entire contents of the text file.

The other elements are mostly blank strings, and some have odd characters that seem to be related to formatting. What am I doing wrong?

1 Answer 1

2
let testString = "Line 1\r\nLine 2\nLine 3\rLine4"
var lines = [String]()
testString.enumerateLines { line, _ in
    lines += [line]
}
println(lines) // [Line 1, Line 2, Line 3, Line4]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! I haven't seen that approach to the problem before. Have a great weekend!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.