gpt4 book ai didi

swift - 在 Swift 中从 NSTextField 获取 NSImage

转载 作者:可可西里 更新时间:2023-11-01 02:27:01 25 4
gpt4 key购买 nike

我曾经像这样从 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
...
}

最佳答案

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

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

关于swift - 在 Swift 中从 NSTextField 获取 NSImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27322169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com