3

I see methods for adding items to a pasteboard. I also see that you can get notifications on pasteboard item removal. But I can't seem to find how to remove an item (both key and value) from a pasteboard. How can an item be removed from a pasteboard?

Ravindranath Akila
  • 209
  • 3
  • 35
  • 45
  • possible duplicate of [How to clear/empty pasteboard on viewWillDisappear](http://stackoverflow.com/questions/11067554/how-to-clear-empty-pasteboard-on-viewwilldisappear), [How to nill the value of UIPasteboard](http://stackoverflow.com/questions/13235528/how-to-nill-the-value-of-uipasteboard) – smileyborg Oct 04 '13 at 02:24
  • Thanks, but it depends. How will my garbage get cleaned up Vs How can I clean up my garbage ;-) – Ravindranath Akila Oct 04 '13 at 07:58
  • I don't think you've read those answers closely enough. – smileyborg Oct 04 '13 at 16:14
  • 2
    I did. Each one of them. Seeing a value to empty or, nil, doesn't count as removing. But, I later found out, you can just grab the "items" property of the pasteboard and reassign it with your own. "The value of the property is an array of dictionaries. Each dictionary represents a pasteboard item, with the key being the representation type and the value the object associated with that type. Setting this property replaces all of the current pasteboard items." Since setting this property actually does a deep copy, that is through archiving. So the items can't be changed locally again. – Ravindranath Akila Oct 05 '13 at 18:48
  • Did the solution outlined in my answer actually address your question? – SwiftArchitect Jan 31 '16 at 01:48

2 Answers2

3

The only difference between the setValue("") and the removeObject() approach is the log provided by UIPasteboard.items. Functionally, either solution will clear your entry, and clear the associated dictionary as well.

pb.contains(pasteboardTypes: [UIPasteboardName.general.rawValue])

will returns false.


You can delete a single entry by manipulating the dictionary, not replacing it by "", unlike How to clear/empty pasteboard on viewWillDisappear.

Option 1

Use this if you want to clear all entries. pb.items will log [].

@IBAction func deleteEntirePasteboard(_ sender: AnyObject) {
    UIPasteboard.remove(withName: UIPasteboardName(rawValue: pasteboardName()))
}

Option 2

Use this to remove an entry and its dictionary. pb.items will also log [] as all entries are cleared.

@IBAction func clearUsingRemove(_ sender: AnyObject) {
    let pb = pasteBoard()
    pb.items = [[String : Any]]()
}

Option 3

This is the generally accepted approach. pb.items will log [{}], showing an empty dictionary. While pb.containsPasteboardTypes will return nil as well, it is arguably not as thorough as the two other options above.

@IBAction func clearUsingQuotes(_ sender: AnyObject) {
    let pb = pasteBoard()
    pb.setValue("", forPasteboardType: UIPasteboardName.general.rawValue)
}

Use this support code to access the UIPasteboard in the 3 solutions listed above:

func pasteBoard() -> UIPasteboard {
    return UIPasteboard(name: UIPasteboardName(rawValue: pasteboardName()),
                        create: true)!
}

func pasteboardName() -> String {
    return "19172176"
}

► Find this solution on GitHub and additional details on Swift Recipes.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
0

I had a similar issue. I needed to clean up several entries without touching other items:

let pasteboard = UIPasteboard.general

pasteboard.items = pasteboard.items.filter{ $0.keys.first != "type.to.be.removed" }

Now use whatever boolean expression in the filter closure to remove items.

Nick Weaver
  • 47,228
  • 12
  • 98
  • 108