Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Xcode project directory from the code

Tags:

xcode

macos

swift

Does anyone know how to get a related Xcode project directory right from the code? I've tried to accomplish it with the pwd environment, but it only works in Build Phases > Run Script.

like image 559
Nikita Ermolenko Avatar asked Mar 16 '26 15:03

Nikita Ermolenko


1 Answers

for individual files you can either use the C-macro __FILE__, or the swift-literal #file.

From the Swift Docs (search for Literal Expressions on the page)

  • #file (String) The name of the file in which it appears.
  • #line (Int) The line number on which it appears.
  • #column (Int) The column number in which it begins.
  • #function (String) The name of the declaration in which it appears.

Once you have the path to the file it should be easy to find your way to the project root.

like image 143
kritzikratzi Avatar answered Mar 18 '26 07:03

kritzikratzi