gpt4 book ai didi

ios - NSFileManager.defaultManager().fileExistsAtPath 返回 false 而不是 true

转载 作者:行者123 更新时间:2023-11-28 08:26:34 26 4
gpt4 key购买 nike

这怎么可能?

let exists = NSFileManager.defaultManager().fileExistsAtPath(path.absoluteString)
print("exists: \(exists)") //false

这是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

你可以看到它就在它应该在的地方:

enter image description here

这是怎么回事?

最佳答案

(此答案中的代码已针对 Swift 3 及更高版本更新。)

显然,您的 path 变量是一个 NSURL(描述文件路径)。获取路径为一个字符串,使用 path 属性,而不是 absoluteString:

let exists = FileManager.default.fileExists(atPath: path.path)

absoluteString 以字符串格式返回 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

关于ios - NSFileManager.defaultManager().fileExistsAtPath 返回 false 而不是 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39815883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com