Bundle File Path and Project Root Path

How to get file path of another bundle? How to get xcode project root path?

The Bundle type knows its own location. See here.

The most reliable way is to derive it from #file. (Beginning in Swift 5.3 it will be called #filePath instead.)

let thisFile = URL(fileURLWithPath: #file)
let projectRoot = thisFile
  .deletingLastPathComponent()
  .deletingLastPathComponent()
  // However many you need to strip off to back out to the root.

This only works because Xcode and SwiftPM happen to provide absolute paths to the compiler. That’s not a given for other build systems.

Thanks Jeremy

Terms of Service

Privacy Policy

Cookie Policy