File IO in Swift 2

Where can I find documentation on doing file IO in Swift 2? I have checked the language reference, but I don't see anything there. Right now I just want to read a file into a string, but I am interested in other file IO as well.


Thanks,


James

Accepted Reply

There is no file API native to Swift, but you can use the Cocoa APIs:


if let data = NSData(contentsOfFile: "/etc/resolv.conf") {
    let string = NSString(data: data, encoding: NSUTF8StringEncoding)
}


or you can use the Unix APIs (open, read, ...), but then you have to do manual memory management.

Replies

There is no file API native to Swift, but you can use the Cocoa APIs:


if let data = NSData(contentsOfFile: "/etc/resolv.conf") {
    let string = NSString(data: data, encoding: NSUTF8StringEncoding)
}


or you can use the Unix APIs (open, read, ...), but then you have to do manual memory management.