Я использую следующий код для получения значка файлов или папок. Затем покажите их в меню. Моя проблема: некоторые файлы, значок не отображается (например, .txt
файл). Значки папок и некоторых других файлов по-прежнему отображаются. Какова возможная причина этой проблемы?
// menuItem.Title: display name for file/folder
// menuItem.Content: full path of file/url
let menuItem = NSMenuItem(title: item.Title, action: #selector(AppDelegate.openLocal(_:)), keyEquivalent: "")
let requiredAttributes = [URLResourceKey.effectiveIconKey]
if let enumerator = FileManager.default.enumerator(at: URL(fileURLWithPath: item.Content), includingPropertiesForKeys: requiredAttributes, options: [.skipsHiddenFiles, .skipsPackageDescendants, .skipsSubdirectoryDescendants], errorHandler: nil) {
while let url = enumerator.nextObject() as? URL {
do {
let properties = try (url as NSURL).resourceValues(forKeys: requiredAttributes)
let icon = properties[URLResourceKey.effectiveIconKey] as? NSImage ?? NSImage()
menuItem.image = icon
}
catch {
}
}
}