Skip to content

Instantly share code, notes, and snippets.

@nemesit
Created June 30, 2014 20:12
Show Gist options
  • Save nemesit/cdfb1706dce65fc9b649 to your computer and use it in GitHub Desktop.
Save nemesit/cdfb1706dce65fc9b649 to your computer and use it in GitHub Desktop.
Swift write to file
import Foundation
let dir = "~/Desktop"
let file = "test.txt"
let path = dir.stringByExpandingTildeInPath
let filePath:NSString = path.stringByAppendingPathComponent(file)
var text: NSString = "random text with no meaning"
var textData: NSData = text.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
var f: CMutablePointer = fopen(filePath.UTF8String, "w".UTF8String)
let success = fwrite(textData.bytes, UInt(1), UInt(textData.length), f)
fsync(fileno(f))
fclose(f)
println("success: \(success)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment