Created
June 30, 2014 20:12
-
-
Save nemesit/cdfb1706dce65fc9b649 to your computer and use it in GitHub Desktop.
Swift write to file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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