美文网首页
Swift小记:读写文件

Swift小记:读写文件

作者: 齐舞647 | 来源:发表于2019-07-28 16:55 被阅读0次
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小记:读写文件

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