Swift: how to expand a tilde in a String path

How can I extend the String path with a tilde in Swift? I have a string like "~/Desktop" and I would like to use this path with the NSFileManager methods, for which the tilde should be expanded to "/Users/<myuser>/Desktop" .

(This question with an explicit expression about the problem does not exist yet, it should be easy to find. Some similar but not satisfying questions: Unable to create a file path in Swift , An easy way to read a local file using Swift?, Tilde-based outlines in Objective-C )

+7
source share
4 answers

Tilde extension

Swift 1

 "~/Desktop".stringByExpandingTildeInPath 

Swift 2

 NSString(string: "~/Desktop").stringByExpandingTildeInPath 

Swift 3

 NSString(string: "~/Desktop").expandingTildeInPath 

Home directory

Alternatively, you can get a home directory like this (returns a String / String? ):

 NSHomeDirectory() NSHomeDirectoryForUser("<User>") 

On Swift 3 and OS X 10.12, you can also use this (returns a URL / URL? ):

 FileManager.default().homeDirectoryForCurrentUser FileManager.default().homeDirectory(forUser: "<User>") 

Edit: In Swift 3.1, this changed to FileManager.default.homeDirectoryForCurrentUser

+24
source

Return string:

 func expandingTildeInPath(_ path: String) -> String { return path.replacingOccurrences(of: "~", with: FileManager.default.homeDirectoryForCurrentUser.path) } 

Return URL:

 func expandingTildeInPath(_ path: String) -> URL { return URL(fileURLWithPath: path.replacingOccurrences(of: "~", with: FileManager.default.homeDirectoryForCurrentUser.path)) } 

If the OS is less than 10.12, replace

 FileManager.default.homeDirectoryForCurrentUser 

with

 URL(fileURLWithPath: NSHomeDirectory() 
+1
source

Here is a solution that is independent of the NSString class and works with Swift 4:

 func absURL ( _ path: String ) -> URL { guard path != "~" else { return FileManager.default.homeDirectoryForCurrentUser } guard path.hasPrefix("~/") else { return URL(fileURLWithPath: path) } var relativePath = path relativePath.removeFirst(2) return URL(fileURLWithPath: relativePath, relativeTo: FileManager.default.homeDirectoryForCurrentUser ) } func absPath ( _ path: String ) -> String { return absURL(path).path } 

Test code:

 print("Path: \(absPath("~"))") print("Path: \(absPath("/tmp/text.txt"))") print("Path: \(absPath("~/Documents/text.txt"))") 

The reason the code is divided into two methods is because you currently prefer URLs when working with files and folders rather than string paths (all newer APIs use URLs for paths).

By the way, if you just want to know the absolute path of ~/Desktop or ~/Documents and similar folders, it will be even easier for you:

 let desktop = FileManager.default.urls( for: .desktopDirectory, in: .userDomainMask )[0] print("Desktop: \(desktop.path)") let documents = FileManager.default.urls( for: .documentDirectory, in: .userDomainMask )[0] print("Documents: \(documents.path)") 
0
source

Quick Expansion 4

 public extension String { public var expandingTildeInPath: String { return NSString(string: self).expandingTildeInPath } } 
0
source

All Articles