美文网首页
Swift 4 读写文本文件

Swift 4 读写文本文件

作者: 冷大大_hawkleng | 来源:发表于2018-06-23 13:21 被阅读470次
let file = "file.txt" // this is the file. we will write to and read from it
let text = "some text" // just a text
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
    let fileURL = dir.appendingPathComponent(file)
    //writing
    do {
        try text.write(to: fileURL, atomically: false, encoding: .utf8)
    }
    catch {
        /* error handling here */
    }
    //reading
    do {
        let text2 = try String(contentsOf: fileURL, encoding: .utf8)
    }
    catch {
        /* error handling here */
    }
}

相关文章

网友评论

      本文标题:Swift 4 读写文本文件

      本文链接:https://www.haomeiwen.com/subject/oybryftx.html