用于NSWorkspace.shared.icon(forFileType:状态的docs:
/*
* Get the icon for a given file type.
*
* The file type may be a filename extension, or a HFS code encoded via NSFileTypeForHFSTypeCode, or a Universal Type Identifier (UTI).
*
* Returns a default icon if the operation fails.
*
*/
// Swift
open func icon(forFileType fileType: String) -> NSImage
// Objective-C
- (NSImage *)iconForFileType:(NSString *)fileType;注意:
如果操作失败,
将返回默认图标。
如何判断操作是否“失败”并返回默认图标?
是否有一种方法可以确定您是否获得了默认图标,而无需进行昂贵的图像或数据比较?
发布于 2020-04-10 15:49:02
快速测试之后,看起来当iconForFileType失败时,每次都会返回相同的指针。这是有意义的,因为它可能只是对“无文件类型”映像的单一共享引用。
因此,您可以用已知的未知文件类型获取该指针一次:
// Do this once, at program startup for example, and keep the reference
NSImage* x = [[NSWorkspace sharedWorkspace] iconForFileType:@".this_is_not_a_file_type"];然后,只需进行指针比较:
NSImage* y = [[NSWorkspace sharedWorkspace] iconForFileType:@".xxx"];
NSLog(@"%p %p", x, y);
if (x == y)
// `iconForFileType` failed发布于 2020-04-12 01:21:02
如果NSWorkspace操作失败,则nil扩展返回icon(forFileType::
extension NSWorkspace {
func iconOptional(forFileType fileType: String) -> NSImage? {
let icon = self.icon(forFileType: fileType)
let iconDefault = self.icon(forFileType: "") // "Returns a default icon if the operation fails."
return icon === iconDefault ? nil : icon
}
}https://stackoverflow.com/questions/60964798
复制相似问题