Write data to file


I have some data and want to write it to a file.


// Create a FileHandle instance

let file: NSFileHandle? = NSFileHandle(forWritingAtPath: "output.txt")

if file != nil {
    // Set the data we want to write
    let data = ("Silentium est aureum" as NSString).dataUsingEncoding(NSUTF8StringEncoding)
    // Write it to the file

    // Close the file
else {
    print("Ooops! Something went wrong!")


Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)