빠른
func saveFile() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as! String
let fileName = "\(documentsDirectory)/textFile.txt"
let content = "Hello World"
content.writeToFile(fileName, atomically: false, encoding: NSUTF8StringEncoding, error: nil)
}
func loadFile() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0] as! String
let fileName = "\(documentsDirectory)/textFile.txt"
let content: String = String(contentsOfFile: fileName, encoding: NSUTF8StringEncoding, error: nil)!
println(content)
}
스위프트 2
func saveFile() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(documentsDirectory)/textFile.txt"
let content = "Hello World"
do{
try content.writeToFile(fileName, atomically: false, encoding: NSUTF8StringEncoding)
}catch _ {
}
}
func loadFile()->String {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(documentsDirectory)/textFile.txt"
let content: String
do{
content = try String(contentsOfFile: fileName, encoding: NSUTF8StringEncoding)
}catch _{
content=""
}
return content;
}
스위프트 3
func saveFile() {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(documentsDirectory)/textFile.txt"
let content = "Hello World"
do{
try content.write(toFile: fileName, atomically: false, encoding: String.Encoding.utf8)
}catch _ {
}
}
func loadFile()->String {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let fileName = "\(documentsDirectory)/textFile.txt"
let content: String
do{
content = try String(contentsOfFile: fileName, encoding: String.Encoding.utf8)
} catch _{
content=""
}
return content;
}