【问题标题】:FileManager: Attribute for when a file was last openedFileManager:文件上次打开时间的属性
【发布时间】:2021-12-24 23:05:46
【问题描述】:

我需要一种检查文件上次打开时间的方法。我尝试通过创建自定义 FileAttributeKey 并将其设置为当前日期,但是当我再次打开文件时,该属性不存在:

private let key = FileAttributeKey(rawValue: "lastOpenedAt")
do {
    try FileManager.default.setAttributes(
        [key: Date()],
        ofItemAtPath: videoNameDirectoryPath
    )
} catch {
    Log.error(error.localizedDescription)
}

所以现在我使用修改日期键来说明我上次打开文件的时间,这并不理想,所以我想知道是否有更好的方法来做到这一点

【问题讨论】:

  • setAttributes 会抛出错误吗?

标签: swift nsfilemanager file-attributes


【解决方案1】:

setAttributes 不支持自定义属性,只能使用documented 的。

要设置自己的属性,您可以使用xattr,如本问题所述:

Write extend file attributes swift example

如果幸运的话,您可以使用来自 Spotlight 的 kMDItemLastUsedDate 又名 MDItem,如 File Metadata Attributes 的文档存档中所述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-20
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多