如何获取 Xcode Playground 的当前工作目录?

dri*_*ing 3 macos swift-playground

我正在使用基于 OSX 的 Playground。当我尝试时,NSFileManager.defaultManager().currentDirectoryPath它返回/这不是我的工作目录。

ska*_*dal 5

这是我找到的最好的方法(在 Zev Eisenberg 的帮助下)。

你需要一点帮助。将文件添加到您的 Playground 资源;您可以通过右键单击“资源”并选择“新建文件”来创建文本文件。将其命名为“Token.txt”以使以下代码起作用。

let tokenPath = Bundle.main.url(forResource: "Token", withExtension: "txt")!.resolvingSymlinksInPath()
let playgroundPath = tokenPath.deletingLastPathComponent().deletingLastPathComponent()
Run Code Online (Sandbox Code Playgroud)

playgroundPath现在将成为您的游乐场的路径。漂亮吧?

(在 Xcode 10 beta 3 上测试。)