在 macOS 上使用 Swift 3 从剪贴板读取

Reading from the clipboard with Swift 3 on macOS

我是 Swift 的初学者,我想知道如何在 macOS (Swift 3) 上读取复制到剪贴板的内容?我搜索了很多,但似乎找不到任何有用的东西。

我从网上尝试过的一些东西:

var pasteboardItems: [NSPasteboardItem]? { get }
print("\(pasteboardItems)")

let pb = NSPasteboard.general()
pb.string(forType: NSPasteboardTypeString)

print("\(pb)")

let pasteboard = UIPasteboard.general
if let string = pasteboard.string {
    // text was found and placed in the "string" constant
}

最后

func paste(sender: AnyObject?) {

    let pasteboard = NSPasteboard.generalPasteboard()

    if let nofElements = pasteboard.pasteboardItems?.count {

        if nofElements > 0 {


            // Assume they are strings

            var strArr: Array<String> = []
            for element in pasteboard.pasteboardItems! {
                if let str = element.stringForType("public.utf8-plain-text") {
                    strArr.append(str)
                }
            }


            // Exit if no string was read

            if strArr.count == 0 { return }


            // Perform the paste operation

            dataSource.cmdPaste(strArr)
       }
    }        
}

适用于 Swift 3Swift 4

// Set string to clipboard
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString("Good Morning", forType: NSPasteboard.PasteboardType.string)

var clipboardItems: [String] = []
for element in pasteboard.pasteboardItems! {
    if let str = element.string(forType: NSPasteboard.PasteboardType(rawValue: "public.utf8-plain-text")) {
        clipboardItems.append(str)
    }
}

// Access the item in the clipboard
let firstClipboardItem = clipboardItems[0] // Good Morning

时代变了。在 Swift 3+ 中你会这样做:(如果你只对字符串感兴趣)

func clipboardContent() -> String?
{
    return NSPasteboard.general.pasteboardItems?.first?.string(forType: .string)
}

另一个解决方案。

class ViewController : NSViewController {

  @IBAction func pasteMenuItemAction(_ sender: NSMenuItem) {
    let p = NSPasteboard.general
    let x = p.readObjects(forClasses: [NSString.self], options: nil)
    let s = x as! [NSString]
    if 0 < s.count {
      print(s[0])
    }
  }

}

该函数 pasteMenuItemAction() 已绑定到“编辑”>“粘贴”菜单项。

我使用 writeObjects() 编辑 > 复制。所以我很自然地在这里使用它的对应物readObjects()

已确认 Xcode 9.2,Swift 4

已添加:

编辑 > 复制的解决方案之一:

  @IBAction func copyMenuItemAction(_ sender: NSMenuItem) {
    let t = "Hello!"
    let p = NSPasteboard.general
    p.clearContents()
    p.writeObjects([t as NSPasteboardWriting])
  }

该函数 copyMenuItemAction() 已绑定到“编辑”>“复制”菜单项。

更简单 ✅ Swift 4:

@IBAction func pasteAction(_ sender: Any) {
    guard let stringToPaste: String = UIPasteboard.general.items.last?.first?.value as? String else { return }
    MYTEXTVIEW.text = stringToPaste
}

另Swift4个解

// Write to pasteboard
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString("Good Morning", forType: NSPasteboard.PasteboardType.string)

// Read from pasteboard
let read = pasteboard.pasteboardItems?.first?.string(forType: .string)