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



Find answers here

Hi there! Ask Us a Question We are ready to help