首页 > 解决方案 > 在 Swift 中,有没有办法知道文件是否正在下载?

问题描述

我正在使用 Swift for macOS 创建一个应用程序。我想知道是否有一种方法可以确定是否正在下载文件,例如图片中的那个。在此处输入图像描述

我尝试了 attributesOfItemAtPath,它返回以下键:

NSFileAttributeKey(_rawValue: NSFileSystemFileNumber)
NSFileAttributeKey(_rawValue: NSFileHFSTypeCode)
NSFileAttributeKey(_rawValue: NSFileReferenceCount)
NSFileAttributeKey(_rawValue: NSFileOwnerAccountName)
NSFileAttributeKey(_rawValue: NSFileCreationDate)
NSFileAttributeKey(_rawValue: NSFileExtensionHidden)
NSFileAttributeKey(_rawValue: NSFileGroupOwnerAccountID)
NSFileAttributeKey(_rawValue: NSFileOwnerAccountID)
NSFileAttributeKey(_rawValue: NSFileModificationDate)
NSFileAttributeKey(_rawValue: NSFileSystemNumber)
NSFileAttributeKey(_rawValue: NSFileGroupOwnerAccountName)
NSFileAttributeKey(_rawValue: NSFilePosixPermissions)
NSFileAttributeKey(_rawValue: NSFileSize)
NSFileAttributeKey(_rawValue: NSFileType)
NSFileAttributeKey(_rawValue: NSFileHFSCreatorCode)
NSFileAttributeKey(_rawValue: NSFileExtendedAttributes)
NSFileAttributeKey(_rawValue: NSFileProtectionKey)

我没有找到相关的条目,所以我尝试使用这个扩展:

extension FileManager {
   
    func getEntireSpotlightInformation(path: String) -> [AnyHashable : Any]? {
           
           guard self.fileExists(atPath: path) else {
               return nil
           }
           
           let cfPath = path as CFString
           let ref = MDItemCreate(kCFAllocatorDefault, cfPath)
          
           
        let attributes = MDItemCopyAttributeNames(ref) as? Array<String>
            
        var returnValue: [AnyHashable : Any]! = Dictionary()

            for (index, item) in attributes!.enumerated() {
                //print("Eccoci - index = \(index) - AttributeName = \(item)")
                let cfAttribute = item as CFString
                let result = MDItemCopyAttribute(ref, cfAttribute)
                returnValue[item] = result
            }
           
           return returnValue
       }
}

其中返回以下键:

kMDItemDateAdded
kMDItemContentCreationDate
kMDItemDocumentIdentifier
kMDItemInterestingDate_Ranking
kMDItemFSFinderFlags
kMDItemContentTypeTree
kMDItemContentType
kMDItemFSNodeCount
kMDItemDisplayName
kMDItemFSCreatorCode
kMDItemFSInvisible
kMDItemLogicalSize
_kMDItemDisplayNameWithExtensions
kMDItemFSIsExtensionHidden
kMDItemContentCreationDate_Ranking
kMDItemFSName
kMDItemContentModificationDate
kMDItemFSCreationDate
kMDItemFSOwnerGroupID
kMDItemPhysicalSize
kMDItemDownloadedDate
kMDItemKind
kMDItemDateAdded_Ranking
kMDItemFSOwnerUserID
kMDItemContentModificationDate_Ranking
kMDItemWhereFroms
kMDItemFSSize
kMDItemFSTypeCode
kMDItemFSContentChangeDate
kMDItemFSLabel

但是,再一次,我看不到任何有希望的东西。任何帮助是极大的赞赏。谢谢

标签: swiftnsfilemanagerspotlight

解决方案


推荐阅读