Swift3 Swift 3将字符串复制到剪贴板

Swift3 Swift 3将字符串复制到剪贴板,swift3,xcode8,macos-sierra,nspasteboard,Swift3,Xcode8,Macos Sierra,Nspasteboard,在使用Xcode 8和Swift 3的macOS 10.12中,如何将字符串复制到剪贴板? 我找不到任何参考资料。Swift 3您这样复制它 let pasteboard = NSPasteboard.general() pasteboard.declareTypes([NSPasteboardTypeString], owner: nil) pasteboard.setString("Good Morning", forType: NSPasteboardTypeString) 在Sw

在使用Xcode 8和Swift 3的macOS 10.12中,如何将字符串
复制到
剪贴板

我找不到任何参考资料。

Swift 3您这样复制它

 let pasteboard = NSPasteboard.general()
 pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
 pasteboard.setString("Good Morning", forType: NSPasteboardTypeString)
在Swift 5.2(macOS 10.15)中,答案略有不同:

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

您可以在该答案中看到我的评论,它不适用于Swift 3,请尝试更新它。这可能很方便。因为是自动完成。对不起,我只需要添加导入应用程序包。多么愚蠢的错误!这不适用于macOS,但iOSOk谢谢,如果您添加import AppKit,我将标记为最佳question@LucaMarconato编辑了答案。我已经试过了,但它不起作用,午饭后我会尝试修复it@LucaMarconato编辑了答案,没有必要导入
AppKit
我已经尝试了这段代码,它工作得很好。