Whenever multiple objects are written to pasteboard using the writeObjects method of NSPasteboard an exception is thrown:
2018-08-25 22:41:46.006537+0200 DragTest[24732:2097487] [General] There are 2 items on the pasteboard, but 1 drag images. There must be 1 draggingItem per pasteboardItem.
2018-08-25 22:41:46.008167+0200 DragTest[24732:2097487] [General] (
0 CoreFoundation 0x00007fff386292fd __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff64460720 objc_exception_throw + 48
2 CoreFoundation 0x00007fff38550f5b +[NSException raise:format:] + 201
3 AppKit 0x00007fff35e06033 -[NSDraggingSession(NSInternal) _initWithPasteboard:image:offset:source:] + 247
4 AppKit 0x00007fff35e05a4b -[NSCoreDragManager dragImage:fromWindow:at:offset:event:pasteboard:source:slideBack:] + 1919
5 AppKit 0x00007fff35e052bd -[NSWindow(NSDrag) dragImage:at:offset:event:pasteboard:source:slideBack:] + 134
6 AppKit 0x00007fff361ee12f -[NSOutlineView dragImage:at:offset:event:pasteboard:source:slideBack:] + 214
7 AppKit 0x00007fff362f6491 -[NSTableView _doImageDragUsingRowsWithIndexes:event:pasteboard:source:slideBack:startRow:] + 656
8 AppKit 0x00007fff362f6925 -[NSTableView __doImageDragUsingRowsWithIndexes:event:pasteboard:source:slideBack:startRow:] + 276
9 AppKit 0x00007fff362f76ba -[NSTableView _performClassicDragOfIndexes:hitRow:event:] + 466
10 AppKit 0x00007fff35e3d890 -[NSTableView _performDragFromMouseDown:] + 474
11 AppKit 0x00007fff35e3bb95 -[NSTableView mouseDown:] + 798
12 AppKit 0x00007fff35e3b660 -[NSOutlineView mouseDown:] + 73
13 AppKit 0x00007fff35b3bb3b -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 5668
14 AppKit 0x00007fff35b09f9f -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2319
15 AppKit 0x00007fff35b094f1 -[NSWindow(NSEventRouting) sendEvent:] + 481
16 AppKit 0x00007fff35aab284 -[NSApplication(NSEvent) sendEvent:] + 336
17 AppKit 0x00007fff35bb44dd -[NSApplication run] + 755
18 AppKit 0x00007fff35b847e7 NSApplicationMain + 780
19 DragTest 0x000000010000387d main + 13
20 libdyld.dylib 0x00007fff6552d091 start + 1
)
This issue is present in the delegates of NSOutlineView, NSCollectionView and even in NSBrowser
When only one object is written it works as expected. Any idea how to avoid this?
It might be worth noting that there is a mention of this exception in the 10.14 Release Notes:
(Look for "Drag & Drop").
However, I find what is written there quite confusing:
I am using tableView:writeRowsWithIndexes:toPasteboard, which causes this exception when dragging multiple items. THe 10.14 Release Notes make it sound like this API is deprecated. I cannot find any mention if it being deprecated though, neither in the documentation nor the header.
As according to the release notes I should be using tableView:pasteboardWriterForRow: instead, I have done this and it fixes the exception.
But - I also need to override the drag image of a multi item drag with a SINGLE image (like seen in Mail.app), and was using
dragImageForRowsWithIndexes:tableColumns:event:offset:, which is no longer called when using pasteBoardWriter. So this unforunately is no solution for me.
None of this change in behaviour (like when dragImageForRowsWithIndexes:tableColumns:event:offset: is called or not called) seems to be documented anywhere, which makes this all quite a frustrating trial and error experience.
If anybody could provide more insight into what is or is not actually deprecated, a bug or a misunderstanding of the API, this would be very helpful.
Cheers
Thomas