NSTreeController, insert, remove, reorder..with Swift Arrays

Hello there,


I am sure people have encountered this issue but I wasn't able to find it around here. Given a NSTableView or a NSOulineView that can be bound with a NSArrayController or a NSTreeController, how does that go with Swift arrays.. Swift Array aren't NSMutableArray, however, removing a selected object seem to work fine. It starts to go wrong when dragging an object to another spot in the collection view.


Here is the stacktrace. But the question is, is there a workaround to achieve this without using NSMutableArray.. Imagine you have a complex strong-typed model with different levels of children.


-[_TtCs21_SwiftDeferredNSArray removeObjectIdenticalTo:]: unrecognized selector sent to instance 0x618000027980
2016-04-15 20:20:21.954 Test[80244:8446095] (
  0   CoreFoundation                      0x00007fff87f394f2 __exceptionPreprocess + 178
  1   libobjc.A.dylib                     0x00007fff900ddf7e objc_exception_throw + 48
  2   CoreFoundation                      0x00007fff87fa31ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
  3   CoreFoundation                      0x00007fff87ea9571 ___forwarding___ + 1009
  4   CoreFoundation                      0x00007fff87ea90f8 _CF_forwarding_prep_0 + 120
  5   AppKit                              0x00007fff93350344 -[NSTreeControllerTreeNode removeObjectFromSubNodesAtIndex:] + 516
  6   AppKit                              0x00007fff932ad41e -[NSTreeController moveNodes:toIndexPath:] + 474
  7   Test                                0x00000001000030e4 _TFC4Test14ViewController11outlineViewfTCSo13NSOutlineView10acceptDropPSo14NSDraggingInfo_4itemGSqPs9AnyObject__10childIndexSi_Sb + 404
  8   Test                                0x00000001000031e4 _TToFC4Test14ViewController11outlineViewfTCSo13NSOutlineView10acceptDropPSo14NSDraggingInfo_4itemGSqPs9AnyObject__10childIndexSi_Sb + 100
  9   AppKit                              0x00007fff93148096 -[NSOutlineView performDragOperation:] + 324
  10  AppKit                              0x00007fff92dc06dc NSCoreDragReceiveMessageProc + 260
  11  HIServices                          0x00007fff94454e4a DoMultipartDropMessage + 328
  12  HIServices                          0x00007fff94454b54 DoDropMessage + 41
  13  HIServices                          0x00007fff94454b25 SendDropMessage + 80
  14  HIServices                          0x00007fff944537f3 DragInApplication + 505
  15  HIServices                          0x00007fff94452611 CoreDragStartDragging + 535
  16  AppKit                              0x00007fff92dbd435 -[NSCoreDragManager _dragUntilMouseUp:accepted:] + 1010
  17  AppKit                              0x00007fff92dba623 -[NSCoreDragManager dragImage:fromWindow:at:offset:event:pasteboard:source:slideBack:] + 1212
  18  AppKit                              0x00007fff92dba155 -[NSWindow(NSDrag) dragImage:at:offset:event:pasteboard:source:slideBack:] + 135
  19  AppKit                              0x00007fff9314706e -[NSOutlineView dragImage:at:offset:event:pasteboard:source:slideBack:] + 221
  20  AppKit                              0x00007fff93212d90 -[NSTableView _doImageDragUsingRowsWithIndexes:event:pasteboard:source:slideBack:startRow:] + 539
  21  AppKit                              0x00007fff93213bdc -[NSTableView _performClassicDragOfIndexes:hitRow:event:] + 329
  22  AppKit                              0x00007fff92dfad95 -[NSTableView _performDragFromMouseDown:] + 463
  23  AppKit                              0x00007fff92df9518 -[NSTableView mouseDown:] + 705
  24  AppKit                              0x00007fff92df901e -[NSOutlineView mouseDown:] + 74
  25  AppKit                              0x00007fff932ce469 -[NSWindow _handleMouseDownEvent:isDelayedEvent:] + 6322
  26  AppKit                              0x00007fff932cf44d -[NSWindow _reallySendEvent:isDelayedEvent:] + 212
  27  AppKit                              0x00007fff92d0e63d -[NSWindow sendEvent:] + 517
  28  AppKit                              0x00007fff92c8eb3c -[NSApplication sendEvent:] + 2540
  29  AppKit                              0x00007fff92af5ef6 -[NSApplication run] + 796
  30  AppKit                              0x00007fff92abf46c NSApplicationMain + 1176
  31  Test                                0x00000001000045a4 main + 84
  32  libdyld.dylib                       0x00007fff900c65ad start + 1
  33  ???                                 0x0000000000000003 0x0 + 3
)


Thank you for your help

Well I know this is 5 years old, but I'm just running into this issue now. Did you ever find a solution? I have an NSTreeController bound to a Swift array and I get the same crash. This time on macOS Monterey.

I encountered this issue when I have a property to bind with treeController @objc dynamic var contents: [AnyObject] = []

I try to change NSMutableArray, works for me.

@objc dynamic var contents: NSMutableArray = NSMutableArray()

NSTreeController, insert, remove, reorder..with Swift Arrays
 
 
Q