SwiftUI snippet to copy text to clipboard

Quick SwiftUI snippet to copy text to clipboard by clicking a button:

Button(action: {
                let pasteboard = NSPasteboard.general
                pasteboard.clearContents()
                pasteboard.writeObjects([self.resultText as NSString])
            }) {
                Text("Copy to Clipboard")
            }.padding()

(self.resultText is your String)

Mastodon