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.
Asked
Active
Viewed 9,186 times
23
-
http://stackoverflow.com/a/34902953/3141234 – Alexander Dec 08 '16 at 12:13
-
you can see my comment in that answer, it does not work for Swift 3 – Nisba Dec 08 '16 at 12:14
-
1So try to update it. The [documentation](https://developer.apple.com/reference/appkit/nspasteboard) would probably be handy. As is autocompletion. – Alexander Dec 08 '16 at 12:15
-
Sorry, I only had to add import AppKit. What a stupid error! – Nisba Dec 08 '16 at 12:16
2 Answers
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
-
-
1
-
-
-
1@LucaMarconato Edited the answer, there is no need to import the `AppKit` I have try this code and it is working perfectly. – Nirav D Dec 08 '16 at 12:30
-
-
@LucaMarconato Welcome mate, I have tried this In Xcode 8 with Swift 3 and it is working without importing `AppKit` may be something else is missing. – Nirav D Dec 08 '16 at 12:42