2014-12-05 64 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 
    ... 
    }