【问题标题】:Swift 3 copy string to clipboardSwift 3 将字符串复制到剪贴板
【发布时间】:2016-12-08 12:12:13
【问题描述】:

您如何在 macOS 10.12 中使用 Xcode 8 和 Swift 3 将字符串 Copy 转换为 Clipboard? 我找不到任何参考资料。

【问题讨论】:

  • 你可以在那个答案中看到我的评论,它不适用于 Swift 3
  • 所以尝试更新它。 documentation 可能会很方便。就像自动完成一样。
  • 对不起,我只需要添加 import AppKit。多么愚蠢的错误!

标签: swift3 xcode8 macos-sierra nspasteboard


【解决方案1】:

Swift 3 你这样复制它。

 let pasteboard = NSPasteboard.general()
 pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
 pasteboard.setString("Good Morning", forType: NSPasteboardTypeString)

【讨论】:

  • 这不是 macOS,而是 iOS
  • 好的,谢谢,如果您添加 import AppKit 我将标记为最佳问题
  • @LucaMarconato 已编辑答案。
  • 我试过了,还是不行,午饭后我会尝试修复它
  • @LucaMarconato 编辑了答案,无需导入AppKit 我已经尝试过这段代码,它运行良好。
【解决方案2】:

在 Swift 5.2(macOS 10.15)中,答案略有不同:

let pasteboard = NSPasteboard.general
pasteboard.declareTypes([.string], owner: nil)
pasteboard.setString("Good Morning", forType: .string)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-21
    • 2010-10-09
    • 1970-01-01
    • 2018-08-19
    • 2018-10-12
    • 1970-01-01
    • 2020-01-30
    • 2010-11-18
    相关资源
    最近更新 更多