3

我想在高层次上做的是使用此代码存储粘贴板上的当前数据(任何类型的数据),我认为这是正确的:

- (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;
}

然后我存储pasteboardItems以备后用。我的问题是,当我尝试将相同的数据写入粘贴板时:

- (void)writeToPasteBoard:(NSArray *)objectsToWrite
{
    [pasteboard clearContents];
    [pasteboard writeObjects:objectsToWrite];
}

我正在做的是用NSPasteboardItem我之前制作的数组传入一个数组。然后我得到这个错误:

Cannot write pasteboard item <NSPasteboardItem: 0x101a11540>.  It is already associated   
with another pasteboard.

我有点困惑,因为NSPasteboardItem我试图写入粘贴板的内容与任何粘贴板都没有关联,因为我自己实例化了它并且从未从粘贴板中检索过它。感谢您的帮助,如果您需要更多详细信息,请告诉我。

4

0 回答 0