For iOS, use string
Setting this property replaces all current items in the pasteboard with the new item. If the first item has no value of the indicated type, nil is returned.
let pasteboard = UIPasteboard.general pasteboard.string = "hello world"
For Mac, use clearContents first
Clears the existing contents of the pasteboard, preparing it for new contents. This is the first step in providing data on the pasteboard.
Sets the given string as the representation for the specified type for the first item on the receiver. This method invokes setData(_:forType:) to perform the write.
let pasteboard = NSPasteboard.general pasteboard.clearContents() pasteboard.setString(text, forType: .string)