首页 > 解决方案 > 为什么 FileManager 和 Finder 不匹配?

问题描述

如果我在模拟器启动时打印文档目录,我会得到:

let simulatorPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
print(simulatorPath)


//prints -> /Users/name/Library/Developer/CoreSimulator/Devices/[device-id]/data/Containers/Data/Application/[application-id]/Documents

如果我用 Finder 打开这个目录,那里还有一些其他文件夹:/Library、/SystemData 和 /tmp。具体来说,/Library/Caches/[domain.app]/[几个缓存文件]。

但是,在我的应用程序中,我正在使用文件管理器创建一个 sqlite 数据库。如果我在创建数据库后执行以下操作:

var path = FileManager.default.currentDirectoryPath + "Library/Caches"

let enumerator = FileManager.default.enumerator(atPath: path)
while let obj = enumerator?.nextObject() as? String {
  print(obj)
}

这是打印的:

ColorSync
ColorSync/com.apple.colorsync.devices
Desktop Pictures
Desktop Pictures/78DJQ81B-3D2C-46C7-A268-3CE1903213FE
Desktop Pictures/78DJQ81B-3D2C-46C7-A268-3CE1903213FE/lockscreen.png
domain.app
domain.app/SQLite
com.apple.cloudkit
com.apple.cloudkit/com.apple.cloudkit.launchservices.hostnames.plist
com.apple.iconservices.store
app
app/SQLite
app/SQLite/cache.db

但是 /Library/Caches/[domain.app] 文件夹的内容在使用枚举器打印时与在 Finder 中访问时不同。我假设错了吗

NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

FileManager.default.currentDirectoryPath + "/Documents"

指向同一个位置?

标签: iosfinderfile-manager

解决方案


在 Finder 中跌跌撞撞后,我发现我正在创建的 SQLite db 文件显示在/Library/Caches,但在我的硬盘驱动器的根目录中。根据文档FileManager.default.currentDirectoryPath:_

启动应用程序时,此属性最初设置为应用程序的当前工作目录。如果由于任何原因当前工作目录不可访问,则此属性的值为 nil。

鉴于此,我认为当我打印该值并/出来时,它相对指的是模拟应用程序的当前目录。如果有人知道为什么会这样,欢迎评论。


推荐阅读