7

As you know for server side swift Perfect 2.0 framework is out. I was trying to get path for root directory but didn't have any luck yet.

Snippet 1

let fileDir = Dir("/Resources/fileuploads")
print(fileDir.path)

this code give me path as

/Resources/fileuploads

Snippet 2

let fileDir = Dir(Dir.workingDir.path + "Resources/fileuploads")
print(fileDir.path)

this code gives me path as

/Users/username/Library/Developer/Xcode/DerivedData/projname-acxmbygsjkthxvbigpfoqesstidh/Build/Products/Debug/Resources/fileuploads/

However actual path where i want it, to point to is.

/Users/username/Documents/folderOne/folderTwo/projname/Resources/fileuploads/

where

/Users/username/Documents/folderOne/folderTwo/projname

is the path where my root folder of the project is located.

Question

how do i get path to root folder of the project ?

Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40

6 Answers6

8

This works in Swift 3.1 and above

// As a string
let THIS_FILES_PATH:String = #file
print(THIS_FILES_PATH) 

// As an array
let THIS_FILES_PATH_AS_ARRAY:[String] = #file.split(separator: "/").map({String($0)})
print(THIS_FILES_PATH_AS_ARRAY) 
Shaheen Ghiassy
  • 7,397
  • 3
  • 40
  • 40
6

You can actually use Foundation with Swift 3 and Perfect 2.

Next, to get your working directory path you will have to use Filemanager.

let fileManager = FileManager.default
let currentPath = fileManager.currentDirectoryPath
print("Current path: \(currentPath)")

In my case, the above snippet (tucked in a handler function) produced the following, when running from terminal:

./.build/debug/AltTabAPI
[INFO] Starting HTTP server on 0.0.0.0:8181 with document root ./webroot
Current path: /Users/rb/ProjectsSwift/AltTabAPI

Works on both Mac and Linux the same... Hope this helps.

Robert Bojor
  • 102
  • 3
  • 1
    is there something i need to change in Xcode's build settings. i am still getting the path as "/Users/username/Library/Developer/Xcode/DerivedData/projname-acxmbygsjkthxvbigpfoqesstidh/Build/Products/Debug" for the code routes.add(method: .get, uri: "/webroot/apiCallTest") { (request, response) in let fileManager = FileManager.default let currentPath = fileManager.currentDirectoryPath print("Current path: \(currentPath)") response.setBody(string: currentPath) response.completed() } – Pawan Joshi Sep 21 '16 at 10:13
2

It seems like you've already sorted out your issue, but if others (like myself) are curious about why you were seeing the Dir.workingDir.path discrepancy, note that if you build and run a Perfect project from the command line (from your root directory, swift build + .build/debut/YourProjectName), the working director will be your projects root directory.

However, if you build and run using Xcode, will be something like /Users/username/Library/Developer/Xcode/DerivedData/projname-acxmbygsjkthxvbigpfoqesstidh/Build/Products/Debug/Resources/.

A bit annoying when trying to build a project that serves static files from Xcode, but good to know.

worthbak
  • 335
  • 3
  • 8
0

If you have issues with static files when building with Xcode, you can add A copy phase in Build setting to copy those file to the executable directory.

-1
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

to get access to folder.. code like below..

let  filePath = paths[0].stringByAppendingString("/image.mov")

Still you need any help feel free to ask me.

JAck
  • 854
  • 9
  • 18
  • I don't it would work. because this is not Cocoa Touch. it's Perfect 2.0 (http://perfect.org) CoreFoundation doesn't apply here, so NSSearchPathForDirectoriesInDomains isn't gonna work. – Pawan Joshi Sep 21 '16 at 07:52
  • is there a way i can access $(PROJECT_DIR) or $(SRCROOT) from swift. without using any CocoaTouch Framework ? – Pawan Joshi Sep 21 '16 at 08:57
  • I am only working with cocoaTouch but I will let you know If I found a solution for this. I dont know what is perfect 2.0 – JAck Sep 21 '16 at 09:00
-6

After a long research, i found out that its unnecessary to get this location. the directory returned by Dir will be enough to store my files.

Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40