Write data to file

 Problem

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



 Recipe


// 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
    file?.writeData(data!)

    // Close the file
    file?.closeFile()
}
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! ;-)

 

 Playground