我应该使用 FileManager 或 NSMetadataQuery 通过 IOS/Swift 应用程序检索 iCloud 中的文件名吗?

发布于 2025-01-17 22:05:24 字数 770 浏览 2 评论 0原文

我看到人们在编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假面具 2025-01-24 22:05:24

看来, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文