我曾经像这样从 Obj-C 中检索 NSTextField 的子类中的 NSImage:
NSDictionary *attributedVal = [[self attributedStringValue] attributesAtIndex:i effectiveRange:&effectiveRange];
if ([[attributedVal allKeys] containsObject:NSAttachmentAttributeName]) {
NSTextAttachment *attachment = [attributedVal valueForKey:NSAttachmentAttributeName];
NSCell *attachmentCell = (NSCell *)[attachment attachmentCell];
... [[attachmentCell image] name] ...
}
当我尝试在 Swift 中做同样的事情时,我似乎无法转换 attachmentCell
但出现编译器错误:
let attributedVal = attributedStringValue.attributesAtIndex(i, effectiveRange: effectiveRange)
if let attachment = attributedVal[NSAttachmentAttributeName] as? NSTextAttachment {
let attachmentCell = attachment.attachmentCell as NSCell // does not work
...
}
最佳答案
感谢内特·库克。以下作品:
let attributedVal = attributedStringValue.attributesAtIndex(i, effectiveRange: effectiveRange)
if let attachment = attributedVal[NSAttachmentAttributeName] as? NSTextAttachment {
let attachmentCell = attachment.attachmentCell as NSTextAttachmentCell
let image = attachmentCell.image
...
}
关于swift - 在 Swift 中从 NSTextField 获取 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27322169/