用 URL 中的波形符替换用户路径

5 cocoa swift

假设一个文件URL包含/Users/me/a/b. 有没有比使用简单的字符串替换NSHomeDirectory()来获取缩写形式更好的方法~/a/b

这是我目前正在使用的

.replacingOccurrences(of: NSHomeDirectory(), with: "~", options: .anchored, range: nil)
Run Code Online (Sandbox Code Playgroud)

PS:没有NSString选角!

DDP*_*DDP 0

转换为 NSString 然后缩写为:

var path = url.path as NSString!
var abbrevPath=path?.abbreviatingWithTildeInPath
Run Code Online (Sandbox Code Playgroud)