Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 653
drinking Avatar asked Oct 26 '25 21:10

drinking


1 Answers

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.)

like image 124
skagedal Avatar answered Oct 28 '25 23:10

skagedal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!