gpt4 book ai didi

swift - 如何在 Mac 上使用 Swift 读取和写入文件?

转载 作者:IT王子 更新时间:2023-10-29 05:33:53 32 4
gpt4 key购买 nike

我一直在整个互联网上寻找这个问题的答案,我得到的每一个答案要么没有用,要么非常复杂,要么只适用于 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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com