这怎么可能?
let exists = NSFileManager.defaultManager().fileExistsAtPath(path.absoluteString) print("exists: \(exists)") //false
这是 path.absoluteString
path.absoluteString
//file:///Users/kuna/Library/Developer/CoreSimulator/Devices/92BD140D-5C14-43C4-80D6-904BB9594ED6/data/Containers/Data/Application/5B818832-BB19-4047-A7F8-1487F36868D6/Documents/wishlists/68/147/128/IMG_0006.PNG
您会看到它应该在这里:
到底是怎么回事?
(此答案中的代码已针对Swift 3及更高版本进行了更新。)
显然,您的path变量是a NSURL(描述文件路径)。要以字符串形式获取路径,请使用path属性,而不是absoluteString:
path
NSURL
absoluteString
let exists = FileManager.default.fileExists(atPath: path.path)
absoluteString以字符串格式(包括file:方案等)返回URL 。
file:
例:
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