4
votes

Is it possible to pass the pointer to an object in a drag and drop operation?

Here is the scenario...

I have an 'NSArray' of custom objects. For arguments sake we'll say this is a list of person objects.

The list is tied to an NSTableView via an NSArrayController. The NSTableView is a drag source and I have an NSView as a drag destination.

When implementing drag and drop I can easily get the name of the currently selected person(s) from the NSArrayController as an NSString in the drag process and pass it to the receiver in the NSView.

However, I would like to be able to pass a pointer to the person object so the receiver has access to the entire object.

The problem seems to be that the drag operation creates a new instance of a person object and does not make a reference to the selected person object despite implementing the required methods in the person model. As a result I get a person object at the destination, it just isn't populated with the data from the object.

I ideally I want the object pointer because the NSView will make use of the object reference in order that any updates to the object itself are reflected in both the NSView and the NSTableView (and everywhere else the object is used).

Is it even possible to use the object reference in a drag and drop operation or do I need to pass some custom reference that is an iVar of the person object and then do a lookup once it arrives at the destination? (This would seem a little archaic to me considering how much object referencing Obj-c has).

I understand that a target could accept a drag operation from outside the application, but specifying the operation as a local operation would account for this would it not?

I've checked the apple docs but still don't seem to be able to find an answer.

Drag and Drop Programming Topics

Pasteboard Programming Guide

Any and all help much appreciated.

1

1 Answers

3
votes

A pasteboard is just a container for data in different types, so whatever you put on the pasteboard must be convertible to data in some type.

What you need is to give each Person object an identifier of some sort and put that on the pasteboard under a custom type identifier of your own invention. Generally, such an identifier should be the bundle identifier of your application (which should be the reverse of your domain name plus your application name—e.g., com.example.surfwriter) plus the name of the type (e.g., com.example.surfwriter.tabstop).

When receiving a drop, check for this type. If it's present, look up the correct object and do whatever you're supposed to with it. A drag from the table view to the same table view should implement this by moving or copying the object to a new row.

Don't use the address of the object—i.e., the object itself—as its identifier. If the object dies (e.g., is deleted), the address will no longer be valid, or may refer to a different object (which may or may not be of the same class as the original object). Use something that you can look up and safely fail to find. This applies to cut/copy and paste more than to drags, but you can and should handle both with pretty much the same code.