Skip to content

Instantly share code, notes, and snippets.

@nbomberger
Forked from frozzare/file.swift
Last active August 29, 2015 14:09
Show Gist options
  • Save nbomberger/6ef39d8902b62ab9c6b8 to your computer and use it in GitHub Desktop.
Save nbomberger/6ef39d8902b62ab9c6b8 to your computer and use it in GitHub Desktop.
import Foundation
class File {
class func exists (path: String) -> Bool {
return NSFileManager().fileExistsAtPath(path)
}
class func read (path: String, encoding: NSStringEncoding = NSUTF8StringEncoding) -> String? {
if File.exists(path) {
return String(contentsOfFile: path, encoding: encoding, error: nil)!
}
return nil
}
class func write (path: String, content: String, encoding: NSStringEncoding = NSUTF8StringEncoding) -> Bool {
return content.writeToFile(path, atomically: true, encoding: encoding, error: nil)
}
}
let read : String? = File.read("/path/to/file.txt")
println(read)
let write : Bool = File.write("/path/to/file2.txt", content: "String to write")
println(write)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment