Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 copy string to clipboard

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.

like image 361
Nisba Avatar asked Dec 08 '16 12:12

Nisba


People also ask

How do you copy text in Swift?

Tap into a text field, highlight the text you want copied and select 'Copy'. Alternatively: Open the Toolbar by tapping the. icon.


2 Answers

Swift 3 you copy it like this way.

 let pasteboard = NSPasteboard.general()
 pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
 pasteboard.setString("Good Morning", forType: NSPasteboardTypeString)
like image 168
Nirav D Avatar answered Oct 19 '22 13:10

Nirav D


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)
like image 24
gepree Avatar answered Oct 19 '22 14:10

gepree