0

I'm writing a number of objects to NSPasteboard for a drag operation using beginDraggingSessionWithItems:event:source::

NSMutableArray *draggingItems = [NSMutableArray array];
for (NSUInteger i = 0; i < numItems; i++) {
    NSPasteboardItem *pasteboardItem = [NSPasteboardItem new];

    [pasteboardItem setDataProvider:[MyItem itemForIndex:i]
                           forTypes:@[@"com.test.FooType"]];

    NSDraggingItem *draggingItem = [[NSDraggingItem alloc] initWithPasteboardWriter:pasteboardItem];
    // ...
    [draggingItems addObject:draggingItem];
}

[sourceView beginDraggingSessionWithItems:draggingItems
                                    event:theEvent
                                   source:self];

And in the dragging destination (in the same app), I try to receive these items using readObjectsForClasses:options:, but I only receive one of them.

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
{
    // sender.draggingPasteboard.pasteboardItems contains all items

    NSArray *myItems = [sender.draggingPasteboard readObjectsForClasses:@[[MyItem class]] options:nil];
    // pasteboard:item:provideDataForType: is called for each object on the pasteboard...
    // ...but this method only returns 1 object (the first one)??

(Note that if you pass @[[NSPasteboardItem class]] for the classes, you get all items back unmodified.)

Any idea why this would this happen? Sample project available here (with lots of NSLogs to see that the items are actually being written to the pasteboard).

jtbandes
  • 115,675
  • 35
  • 233
  • 266

1 Answers1

0

Turns out the implementation of -pasteboard:item:provideDataForType: needs to set data on the item, not on the pasteboard — doing the latter overwrites previously written data for other items.

jtbandes
  • 115,675
  • 35
  • 233
  • 266