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

iOS

simple text file creation in iOS using swift, read, write, etc.

How does one create, open, write to, close, reopen and read plain text files in iOS using the latest Swift in xcode.

2 Answers

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!)

// it worked for me:)

It it possible to read Xcode server bot (.log) files this way?