"Bash like" NSURL 或 Foundation 路径的路径

"Bash like" paths to NSURL or Foundation path

在大多数命令行工具中,可以使用以下格式指定文件路径:../someFile~/anotherFile/foo/bar。我怎样才能从这样的路径中初始化一个有效的 Swift URL 或 (Foundation) 路径?

编辑:
也许代码示例更清楚,假设我想从路径初始化一个字符串并且我的用户目录中有一个 foo 文件,这样做:

try String(contentsOfFile: "~/foo")

不起作用(错误是文件不存在)

在 Swift 中,您可以:

let str = "~/Documents"
// NSString has a function for decoding this, not available to String:
let str2 = (str as NSString).standardizingPath as String 

// Swift deprecated the String path manipulation functions,
// but supplies them as part of NSURL:
let url = URL(fileURLWithPath: str)
let url2 = url.standardizedFileURL // Expands the URL