因为异常'NSInvalidArgumentException'而取消拖动:尝试插入nil对象

ane*_*yzm 6 macos cocoa objective-c

当我尝试将一个对象从一个对象拖放NSOutlineView到另一个对象时,它通常可以工作,但有时它会因某些对象而失败.我收到消息:

*** Canceling drag because exception 'NSInvalidArgumentException' (reason '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]') was raised during a dragging session
Run Code Online (Sandbox Code Playgroud)

我发现当我第一次将项目拖动到同一个表的另一行(不释放它)时会发生这种情况,然后拖动到新表.

这是我在拖动过程中可以检查的最后一个函数中draggedItems的内容:

- (id <NSPasteboardWriting>)outlineView:(NSOutlineView *)outlineView pasteboardWriterForItem:(id)item
{
    CBCollectible *collectible = [item representedObject];
    return [[collectible UniqueID] stringValue];
}

- (void)outlineView:(NSOutlineView *)outlineView
    draggingSession:(NSDraggingSession *)session
       endedAtPoint:(NSPoint)screenPoint
          operation:(NSDragOperation)operation
{
    //CBDebug(@"end draggingLeaderIndex %i", session.draggingLeaderIndex);
}

- (void)outlineView:(NSOutlineView *)outlineView
    draggingSession:(NSDraggingSession *)session
   willBeginAtPoint:(NSPoint)screenPoint
           forItems:(NSArray *)draggedItems
{
    FreeAndNil(theDraggedItems);
    theDraggedItems = [[NSArray alloc] initWithArray:draggedItems];
    [session.draggingPasteboard setData:[NSData data] forType:MAIN_VIEW_PASTEBOARD_TYPE];
}

- (void)outlineView:(NSOutlineView *)outlineView
updateDraggingItemsForDrag:(id<NSDraggingInfo>)draggingInfo
{
}
Run Code Online (Sandbox Code Playgroud)

输出日志:

NSLog(@"draggedItems %@", draggedItems);
<__NSArrayM 0x31fa0f0>(
<NSTreeControllerTreeNode: 0x1d23750>, child nodes {}
)
Run Code Online (Sandbox Code Playgroud)

更新:拖动注册

[outlineView1 registerForDraggedTypes:@[MAIN_VIEW_PASTEBOARD_TYPE, NSStringPboardType, NSFilenamesPboardType]];
[outlineView2 registerForDraggedTypes:@[MAIN_VIEW_PASTEBOARD_TYPE, NSStringPboardType, NSFilenamesPboardType]];
Run Code Online (Sandbox Code Playgroud)