Get the relative path in swift

Viewed 49

I have a swift file on my server that I access using an absolute path. Eventually, the path will change when I'll host the server somewhere.


The current path:

 "/Users/user/projects/ios-projects/project-name/resources/myfolder/myfile"

What I want to achieve:

"../../../resources/myfolder/myfile"

I've looked into relative paths in swift, but everything looks outdated and suggests using 3rd party libraries.

Also some suggested using FileManager.default.currentDirectoryPath but it is giving me a different path.

FileManager.default.currentDirectoryPath

"/Users/user/Library/Developer/Xcode/DerivedData/projectname-ebbjtdinidfrryclqgpbyitmljjo/Build/Products/Debug"

Is there a way to get the desired relative path using FileManager?

1 Answers

/Users/user points to the home directory of user user.

You get the URL of the home folder of the current user with

let homeDirectory = URL(fileURLWithPath: NSHomeDirectory())

But this doesn't work in a sandboxed environment.

Related