Swift 3 Read Terminal Current Working Directory

I want to develop an application to advance the experience when using the terminal on Mac. And I need to want to get the current working directory (cwd) from the terminal. How can I implement this? I notice that the answer in " Get terminal output" after swift command is really good, but it still seems like it still can't completely solve my problem. I've looked into Process (). CurrentDirectoryPath from Apple doc, https://developer.apple.com/reference/foundation/process. Does this do what I need? Can I use it like this?

let path = Process().currentDirectoryPath

      

I am very new to Swift and Xcode, please help me! Thank!


Update: thanks guys! It seems that both

let path = fileManager.default.currentDirectoryPath

      

and Process () alone temporarily works for me. Are there any differences between the two?

+6


source to share


1 answer


I had the same question and chose the last option:

let path = FileManager.default.currentDirectoryPath

      

To answer your updated question, the difference between the Process()

and approaches FileManager

is that the approach Process().currentDirectoryPath

prepares to create a new subprocess and then tells you the working directory that the subprocess will use. The subprocess will of course have the same working directory as the parent process, but create a new instance Process

just to find the working directory.



FileManager

approach FileManager

as it returns the working directory that is being used by the current process without creating any subprocesses or any other overhead.

Sources:
https://developer.apple.com/documentation/foundation/filemanager/1409234-default
https://developer.apple.com/documentation/foundation/filemanager/1410766-currentdirectorypath

+7


source







All Articles