我一直在整个互联网上寻找这个问题的答案,我得到的每一个答案要么没有用,要么非常复杂,要么只适用于 iOS,要么三者的结合。我只是在寻找一种在 Swift 中执行文件 I/O 以便在 Mac 上使用的简单方法。所以我想混合 c++ 和 swift 的方法也可以,但为此我遇到了和以前一样的问题。任何帮助将不胜感激!
最佳答案
Abhi Beckert's代码更新为 Swift 3:
import Cocoa
var str = "Hello, playground"
// get URL to the the documents directory in the sandbox
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as NSURL
// add a filename
let fileUrl = documentsUrl.appendingPathComponent("foo.txt")
// write to it
try! str.write(to: fileUrl!, atomically: true, encoding: String.Encoding.utf8)
关于swift - 如何在 Mac 上使用 Swift 读取和写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25275882/