问题
What I am trying to do on a high level is store the current data that is on the pasteboard (data of any type) using this code, which I believe to be correct:
- (NSArray *)readFromPasteBoard
{
NSMutableArray *pasteboardItems = [NSMutableArray array];
for (NSPasteboardItem *item in [pasteboard pasteboardItems]) {
//Create new data holder
NSPasteboardItem *dataHolder = [[NSPasteboardItem alloc] init];
//For each type in the pasteboard's items
for (NSString *type in [item types]) {
//Get each type's data and add it to the new dataholder
NSData *data = [[item dataForType:type] mutableCopy];
if (data) {
[dataHolder setData:data forType:type];
}
}
[pasteboardItems addObject:dataHolder];
}
return pasteboardItems;
}
I then store pasteboardItems
for later use. My problem is that when I try to write this same data to the pasteboard:
- (void)writeToPasteBoard:(NSArray *)objectsToWrite
{
[pasteboard clearContents];
[pasteboard writeObjects:objectsToWrite];
}
What I am doing is passing in an array with the NSPasteboardItem
I made earlier. I then get this error:
Cannot write pasteboard item <NSPasteboardItem: 0x101a11540>. It is already associated
with another pasteboard.
I'm a little bit confused, because the NSPasteboardItem
I am trying to write to the pasteboard isn't associated with any pasteboard as I instantiated it myself and never retrieved it from the pasteboard. Thanks for your help, please let me know if you need any more details.
来源:https://stackoverflow.com/questions/15696907/dissociating-nspasteboarditem-from-pasteboard