(此答案中的代码已针对Swift 3及更高版本进行了更新。)
显然,您的
path变量是a
NSURL(描述文件路径)。要以字符串形式获取路径,请使用
path属性,而不是
absoluteString:
let exists = FileManager.default.fileExists(atPath: path.path)
absoluteString以字符串格式(包括
file:方案等)返回URL 。
例:
let url = URL(fileURLWithPath: "/path/to/foo.txt")// This is what you did:print(url.absoluteString)// Output: file:///path/to/foo.txt// This is what you want:print(url.path)// Output: /path/to/foo.txt



