在作業系統的某些檔案中存在“上次打開”屬性:

修改和打開屬性可以通過以下方式獲取:
//modified date
try? FileManager.default.attributesOfItem(atPath: url.path)[FileAttributeKey.modificationDate] as? Date
//creation date
try? FileManager.default.attributesOfItem(atPath: url.path)[FileAttributeKey.creationDate] as? Date
但是如何獲得“最后打開”日期?
uj5u.com熱心網友回復:
Leo 在評論中建議使用另一個答案URLResourceValues.contentAccessDate可能是最干凈的方式,尤其是因為您已經有一個 URL,而如今通常是這種情況。
func lastAccessDate(forURL url: URL) -> Date?
{
return try? url.resourceValues(
forKeys: [.contentAccessDateKey]).contentAccessDate
}
您還可以使用以下路徑進入 BSD 層:
import Darwin // or Foundation
func lastAccessDate(forFileAtPath path: String) -> Date?
{
return path.withCString
{
var statStruct = Darwin.stat()
guard stat($0, &statStruct) == 0 else { return nil }
return Date(
timeIntervalSince1970: TimeInterval(statStruct.st_atimespec.tv_sec)
)
}
}
resourceValues如果指定的 URL 是符號鏈接,我不是 100% 的行為,但stat()會回傳有關鏈接指向的檔案系統 inode 的資訊。如果您想直接了解有關鏈接本身的資訊,請lstat()改用。stat()其他方面lstat()也一樣。
uj5u.com熱心網友回復:
MDItem 可用于獲取這些引數——kMDItemLastUsedDate
extension URL {
var lastOpenDate: Date? {
guard isFileURL else { return nil }
return MDItemCopyAttribute(MDItemCreateWithURL(kCFAllocatorDefault, self as CFURL), kMDItemLastUsedDate) as? Date
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/514429.html
標籤:迅速苹果系统文件
