Brand new to Swift, but done a fair amount of coding in other languages (Java, C, Python...) and I'm frustrated at how seemingly difficult it is to simply read in a file.
I've read a number of posts, like this one, but can't get their suggestions to work. Also read this post and this one but the solutions seemed long for such a simple task and also didn't work for me.
Here's a simple version:
import Foundation
let path = "./data.txt"
let fileContents = String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
print(fileContents) // prints out: ("./data.txt", 4)
let lines : [String] = fileContents.componentsSeparatedByString("\n") // error: value of type 'String' has no member 'componentsSeparatedByString'
Now, based on the first post I linked above, I expected this to read the specified file into a single string, then split the string on newlines and give me an array with each line as an element. Instead, when I print fileContents, I get some type of tuple rather than the string I expected. And when I try to split the string, apparently that function doesn't exist.
Can someone explain what I'm missing and show me a short, simple way to read the lines from a file into a string array? I'm reading the documentation but can't even find an explanation of the String() call in line 3 or reference to the componentsSeparatedByString() in line 5 so I'm going totally off the suggestions in that first post; any pointers are much appreciated.