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.
Swift 3 copy string to clipboard
Asked Answered
https://mcmap.net/q/433407/-writing-a-string-to-nspasteboard –
Yul
you can see my comment in that answer, it does not work for Swift 3 –
Edan
So try to update it. The documentation would probably be handy. As is autocompletion. –
Yul
Sorry, I only had to add import AppKit. What a stupid error! –
Edan
Swift 3 you copy it like this way.
let pasteboard = NSPasteboard.general()
pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
pasteboard.setString("Good Morning", forType: NSPasteboardTypeString)
This is not for macOS but iOS –
Edan
Ok thank you, if you add import AppKit I will mark as best question –
Edan
@LucaMarconato Edited answer for that. –
Pickard
I have tried and it does not work, after lunch I will try to fix it –
Edan
@LucaMarconato Edited the answer, there is no need to import the
AppKit
I have try this code and it is working perfectly. –
Pickard Working perfectly (I still have to add AppKit) –
Edan
@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. –
Pickard 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)
Swift 3 you copy it like this way.
let pasteboard = NSPasteboard.general()
pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
pasteboard.setString("Good Morning", forType: NSPasteboardTypeString)
This is not for macOS but iOS –
Edan
Ok thank you, if you add import AppKit I will mark as best question –
Edan
@LucaMarconato Edited answer for that. –
Pickard
I have tried and it does not work, after lunch I will try to fix it –
Edan
@LucaMarconato Edited the answer, there is no need to import the
AppKit
I have try this code and it is working perfectly. –
Pickard Working perfectly (I still have to add AppKit) –
Edan
@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. –
Pickard © 2022 - 2024 — McMap. All rights reserved.