2014-12-05 66 views
2

我用来检索NSImage中从对象 - 这样的NSTextField的一个子类:从获取的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] ... 
    } 

当我尝试做相同的斯威夫特我似乎无法成为能够铸attachmentCell,但得到一个编译器错误:

let attributedVal = attributedStringValue.attributesAtIndex(i, effectiveRange: effectiveRange) 
    if let attachment = attributedVal[NSAttachmentAttributeName] as? NSTextAttachment { 
    let attachmentCell = attachment.attachmentCell as NSCell // does not work 
    ... 
    } 
+2

您可以改为使用'NSTextAttachmentCell'吗? 'attachment.attachmentCell'在Swift中是'NSTextAttachmentCellProtocol'类型,'NSCell'不符合'NSTextAttachmentCell'。 – 2014-12-05 18:55:08

回答

1

感谢内特库克。以下作品:

let attributedVal = attributedStringValue.attributesAtIndex(i, effectiveRange: effectiveRange) 
    if let attachment = attributedVal[NSAttachmentAttributeName] as? NSTextAttachment { 
    let attachmentCell = attachment.attachmentCell as NSTextAttachmentCell 
    let image = attachmentCell.image 
    ... 
    }