drinking
drinking

Reputation: 1553

How to get current working directory of a Xcode Playground?

I'm using a Playground based on OSX. When I tried NSFileManager.defaultManager().currentDirectoryPath it returns / which is not my working directory.

Upvotes: 4

Views: 1398

Answers (1)

skagedal
skagedal

Reputation: 2401

This is the best way I've found (with the help of Zev Eisenberg).

You need a little bit of help. Add a file to your playground's resources; you can just create a text file by right-clicking "Resources" and choose "New File". Call it "Token.txt" for the following code to work.

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

playgroundPath will now be the path of your playground. Pretty, eh?

(Tested on Xcode 10 beta 3.)

Upvotes: 6

Related Questions