【问题标题】:Why is there a mismatch between FileManager and Finder?为什么 FileManager 和 Finder 不匹配?
【发布时间】:2018-11-21 15:58:59
【问题描述】:

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

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"

指向同一个位置?

【问题讨论】:

    标签: ios finder file-manager


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 2018-07-14
      • 2015-10-03
      • 2011-03-17
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多