2014-12-05 2 views
2

я использовал для получения NSImage в подклассе NSTextField от Obj-C, как это:Получить NSImage от NSTextField в Swift

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 я не могу быть возможно получить коммандер

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' имеет тип' NSTextAttachmentCellProtocol' в Swift, который 'NSCell' не соответствует, но' NSTextAttachmentCell' делает. –

ответ

1

Благодаря Nate Cook. Следующие работы:

let attributedVal = attributedStringValue.attributesAtIndex(i, effectiveRange: effectiveRange) 
    if let attachment = attributedVal[NSAttachmentAttributeName] as? NSTextAttachment { 
    let attachmentCell = attachment.attachmentCell as NSTextAttachmentCell 
    let image = attachmentCell.image 
    ... 
    } 
Смежные вопросы