Dissociating NSPasteboardItem from pasteboard

女生的网名这么多〃 提交于 2019-12-11 03:32:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!