Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get path of root directory of your project folder in Perfect 2.0?

Tags:

swift

perfect

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 ?

like image 259
Pawan Joshi Avatar asked Sep 21 '16 07:09

Pawan Joshi


1 Answers

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) 
like image 196
Shaheen Ghiassy Avatar answered Sep 28 '22 08:09

Shaheen Ghiassy