【发布时间】:2014-12-05 18:13:40
【问题描述】:
我曾经像这样从 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
...
}
【问题讨论】:
-
您可以转为
NSTextAttachmentCell吗?attachment.attachmentCell在 Swift 中属于NSTextAttachmentCellProtocol类型,NSCell不符合,但NSTextAttachmentCell符合。
标签: swift nstextfield nscell nstextattachment