我应该使用 FileManager 或 NSMetadataQuery 通过 IOS/Swift 应用程序检索 iCloud 中的文件名吗?
我看到人们在编写 IOS/Swift 应用程序时使用 FileManager 和 NSMetadataQuery 来检索 iCloud 中的文件名。哪个更好、更安全? Apple 似乎希望您使用 NSMetadataQuery,但 FileManager 的优点是代码也可以在 IOS 设备和 iCloud 上运行,并且除了文件名之外还提供文件属性。
例如使用 FileManager 可以这样完成:
let contents =
try FileManager.default.contentsOfDirectory(at: directoryURL,
includingPropertiesForKeys: nil,
options: [.skipsHiddenFiles])
for file in contents {
…
}
使用 NSMetadataQuery 像这样:
self.nsMetadataQuery!.predicate = NSPredicate(format: "%K LIKE '*'",
// argumentArray: [NSMetadataItemPathKey, path, NSMetadataItemFSNameKey])
I see people are using the FileManager and NSMetadataQuery to retrieve filenames in iCloud, when programming an IOS/Swift app. Which is better, and safer ? It seems Apple wants you to use NSMetadataQuery, but the advantage for the FileManager, is that the code will also work on the IOS device as well as iCloud, and also provides file attributes in addition to filenames.
For example using FileManager it can be done this way:
let contents =
try FileManager.default.contentsOfDirectory(at: directoryURL,
includingPropertiesForKeys: nil,
options: [.skipsHiddenFiles])
for file in contents {
…
}
Using NSMetadataQuery like this:
self.nsMetadataQuery!.predicate = NSPredicate(format: "%K LIKE '*'",
// argumentArray: [NSMetadataItemPathKey, path, NSMetadataItemFSNameKey])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来, Apple Docs 鼓励您使用
NSMetadataQuery
用于在 iCloud 容器而不是文件管理器 API 中搜索文档。因此,
NSMetaDataQuery
可能是正确的选择。It seems, that Apple Docs encourage you to use
NSMetadataQuery
for searching documents in iCloud Container instead of File Manager API.So probably
NSMetaDataQuery
is the way to go.