为什么FileManager和Finder之间不匹配?

时间:2018-11-21 15:59:00

标签: ios finder file-manager

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

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"

指向同一位置?

1 个答案:

答案 0 :(得分:0)

在Finder上徘徊了一段时间后,我发现正在创建的SQLite数据库文件显示在/Library/Caches中,但显示在硬盘的根目录中。根据{{​​3}}的FileManager.default.currentDirectoryPath

  

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

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

相关问题