23

How do you Copy a string to Clipboard in macOS 10.12 with Xcode 8 and Swift 3? I am not able to find any reference.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Nisba
  • 3,210
  • 2
  • 27
  • 46

2 Answers2

30

In Swift 5.2 (on macOS 10.15) the answer is slightly different:

let pasteboard = NSPasteboard.general
pasteboard.declareTypes([.string], owner: nil)
pasteboard.setString("Good Morning", forType: .string)
gepree
  • 617
  • 7
  • 9
26

Swift 3 you copy it like this way.

 let pasteboard = NSPasteboard.general()
 pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
 pasteboard.setString("Good Morning", forType: NSPasteboardTypeString)
Nirav D
  • 71,513
  • 12
  • 161
  • 183