1

I'm writing a swift terminal program (let's say it's called "myProject") that requires me to save some files. I would like the files to be saved within the project folder programmatically.

I've seen in this post there are some environment variables like $(PROJECT_DIR) that give the path to the project folder. I've already checked that XCode does have the $(PROJECT_DIR) variable using:

$ xcodebuild -project myProject.xcodeproj -target "myProject" -showBuildSettings

But I'm not exactly sure how I can use this variable in my actual code. Like if I tried

print($(PROJECT_DIR))

I would just get the errors:

use of unresolved identifier '$' 
use of unresolved identifier 'PROJECT_DIR'

How do I get the path to my project folder programmatically?

Community
  • 1
  • 1
Jeffrey Chen
  • 225
  • 3
  • 16

1 Answers1

0

Those aren't environment variables that are available to your program at runtime; they're used internally by Xcode for the build process.

Your program is located at Bundle.main.executableURL (NSBundle.mainBundle().executableURL in Swift 2) at runtime (executablePath is also available if you prefer).

If you have additional files that your program needs to access, they must be in a known location. You can use a standard directory in the filesystem such as /usr/local/bin, or you can create a bundle and copy those resources into the bundle as part of the build process.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Thanks for clarifying. But NSBundle.mainBundle().executableURL doesn't give me the path of the project folder that I wanted. It gives me: "/Users/_____/Library/Developer/Xcode/DerivedData/swiftclaw-ejedpqkbwdlfzbdmdfbmlwxroxft/Build/Products/Debug/myProject" Instead I wanted: "/Users/_____/Desktop/myProject or wherever the location of the myProject.xcodeproj file is – Jeffrey Chen Aug 27 '16 at 21:37
  • Why does your compiled program need the directory where its source code is located? – jscs Aug 27 '16 at 21:39
  • There's a python script in the project folder that graphs the output of the files – Jeffrey Chen Aug 27 '16 at 23:01
  • You should put that script into a standard location like /usr/local/bin, then, and access it there. Even if you do get the project's directory into the executable, it will be baked-in; if the directory moves after you've compiled, this procedure will fail. – jscs Aug 27 '16 at 23:08
  • How will the project's directory be baked-in? It should change as the directory moves, which is why I'm looking for how to do it. Otherwise I would just type the path in manually. – Jeffrey Chen Aug 28 '16 at 00:43
  • The `$PROJECT_DIR` variable you're looking at doesn't exist past build time. There is no concept of a "project" or a "project directory" once your program is compiled. – jscs Aug 28 '16 at 01:05
  • Oh okay, thanks for clarifying that. Then I guess there is no way to get what I'm looking for? – Jeffrey Chen Aug 28 '16 at 01:09
  • No, you have to have to have a known location (even if it's only relative to something else, like the Documents directory, or your app's executable). – jscs Aug 28 '16 at 01:24