Re: Can't drag a custom data object.
Re: Can't drag a custom data object.
- Subject: Re: Can't drag a custom data object.
- From: Jens Alfke <email@hidden>
- Date: Mon, 26 Apr 2010 07:46:12 -0700
On Apr 26, 2010, at 5:58 AM, Billy Flatman wrote:
> I'm trying to put a custom object into past board in order to perform a drag operation.
You can’t do that, for the same reason you can’t directly write a custom object to a file. The pasteboard basically stores bytes, so it has to be able to convert what you store in it to and from a stream of bytes. There are a limited number of data types it understands — strings, numbers, arrays, dictionaries and of course raw data.
> ** This doesn't (actually stops drag and drop working for my application until I logout).
> [pboard setData:[items objectAtIndex:0] forType:kOutlineViewGroupType];
You’re passing the wrong type of object to the setData: parameter. Look at the header or the docs — it takes an NSData. You’re passing it some other custom object. The result is going to be an exception or a crash. (The reason this doesn’t produce a warning at compile time is because -objectAtIndex: just returns type id, which is compatible with anything (it’s like void* for objects.) You have to pay attention to what type of object you know is in the array.
If you want to put a custom object in the pasteboard, there are several ways:
(1) Wrap it in an NSValue via [NSValue valueWithPointer:]. This is kind of dangerous because you have to make sure the object won’t get dealloced before the pointer is eventually used, since -valueWithPointer: doesn’t retain the object.
(2) Store some value that refers to the object, like a row number or an access key. You have to make sure that value will continue to work, i.e. the row number remains correct.
(3) Implement a way to serialize the object into data or a dictionary/array. Implementing the NSCopying protocol is a standard way to do that.
The first two techniques are OK for drag-n-drop because the lifetime of the pasteboard is limited. If you want to implement copy and paste you probably need to go with #3 because the pasteboard could stay around for an arbitrary amount of time.
—Jens_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden