Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Josh Milbrandt
6,209 Pointshaving a hard time reading a text file in swift
I have a .txt file, was an rtf file that I renamed in text editor (not sure if that matters?) the entire text file is just numbers
34856 32345 35567 23344
when I drag and drop the file location that comes up in swift is: /Users/jdmpurple/Desktop/TypeID.txt
can someone tell me the code that will allow me to read all of the numbers into an Array? (there are about 20,000 numbers)
I tried adapting this code that was posted a week ago on the website:
let file = "file.txt" if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] { let dir = dirs[0] //documents directory let path = dir.stringByAppendingPathComponent(file); let text = "some text"
//writing
text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding, error: nil);
//reading
let text2 = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
println(text2!)
}
but I wasn't able to get it to work by changing the path of file.txt, it comes up with nil
I am trying to do this in a playground i'd love to get something I copy paste that will work so that I can play around with it and learn how to expand on it, i'm kind of embarrassed I just can't get it to work