Search code examples
drag-and-dropnstableviewmacos-mojave

Mojave - Drag and Drop from NSTableView fails if more items are written than rows dragged


I'm getting this error when dragging multiple items in OSX Mojave:

[General] There are 2 items on the pasteboard, but 1 drag images. There must be 1 draggingItem per pasteboardItem.
2018-09-05 14:09:57.176486+0200 TableViewPasteboardCrash[3106:66642] [General] (
    0   CoreFoundation                      0x00007fff4c1dd43d __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff7802b720 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff4c1dd26f +[NSException raise:format:] + 201
    3   AppKit                              0x00007fff49a03387 -[NSDraggingSession(NSInternal) _initWithPasteboard:image:offset:source:] + 247
    4   AppKit                              0x00007fff49a02d9f -[NSCoreDragManager dragImage:fromWindow:at:offset:event:pasteboard:source:slideBack:] + 1919
    5   AppKit                              0x00007fff49a02611 -[NSWindow(NSDrag) dragImage:at:offset:event:pasteboard:source:slideBack:] + 134
    6   AppKit                              0x00007fff49eb9a75 -[NSTableView _doImageDragUsingRowsWithIndexes:event:pasteboard:source:slideBack:startRow:] + 656
    7   AppKit                              0x00007fff49eb9f09 -[NSTableView __doImageDragUsingRowsWithIndexes:event:pasteboard:source:slideBack:startRow:] + 276
    8   AppKit                              0x00007fff49ebac9e -[NSTableView _performClassicDragOfIndexes:hitRow:event:] + 466
    9   AppKit                              0x00007fff49a31cf8 -[NSTableView _performDragFromMouseDown:] + 474
    10  AppKit                              0x00007fff49a2fffc -[NSTableView mouseDown:] + 798
    11  AppKit                              0x00007fff498b46ef -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 5668
    12  AppKit                              0x00007fff497e872f -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2319
    13  AppKit                              0x00007fff497e7bd5 -[NSWindow(NSEventRouting) sendEvent:] + 481
)

Does anyone have the same problem or a solution to drag multiple items ?


Solution

  • It looks like a bug in macOS 10.14.
    There's actually a rdar for that: https://openradar.appspot.com/44135683

    Meanwhile, there's a simple workaround.

    I assume you are using NSPasteboard.writeItems, with a custom data class implementing NSPasteboardWriting.

    I can definitely confirm this crashes on macOS 10.14.

    Instead of NSPasteboardWriting, you should adopt NSCoding.

    This means implementing:

    Then, instead of using NSPasteboard.writeItems, use NSPasteboard.setPropertyList:

    let items = [ MyItem ]() /* MyItem conforms to NSCoding */
    
    /* Retrieve items */
    
    pboard.setPropertyList( items, forType: "someTypeString" )
    

    This won't crash with multiple items.
    You'll then be able to retrieve your items with NSPasteboard.propertyList:

    let items = pboard.propertyList( forType: "someTypeString" ) as? [ MyItem ]