2014-12-05 9 views
2

에 NSTextField있는에서 NSImage을 얻기이 같은의 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] ... 
    } 

내가 할 수없는 것 스위프트에 동일한 작업을 수행하려고

네이트 쿡

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'는 않습니다. –

답변

1

감사 : attachmentCell 캐스팅하지만 컴파일러 오류를 얻을 수. 다음 작품들 :

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