Tal Zion
Tal Zion

Reputation: 6526

How to get Xcode root folder directory path using Swift

How to get the Xcode project root directory?

Example: /Users/username/MyProjects/Project/. similar to ${SRCROOT}

I checked through FileManager urls and couldn't find the path.

Goal

I am running tests and would like to create a log.txt file in the project root directory.

Upvotes: 4

Views: 3879

Answers (1)

Gereon
Gereon

Reputation: 17844

AFAIK there is no direct way to access this, but what you can do is this for simulator builds:

Modify your xcode scheme to add an environment var SRCROOT with the value ${SRCROOT}

In your code, get the system environment using ProcessInfo:

let srcroot = ProcessInfo.processInfo.environment["SRCROOT"]

Upvotes: 4

Related Questions