Swift对文件的简单读写操作

2021-10-29 10:45:28 浏览数 (1)

代码语言:javascript复制
let file = "file.txt" 
if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] { 
    let dir = dirs[0] //documents directory 
    let path = dir.stringByAppendingPathComponent(file); 
    let text = "some text" //writing
    text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding, error: nil); //reading 
    let text2 = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil) 
}

0 人点赞