• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Can't drag a custom data object.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't drag a custom data object.


  • Subject: Re: Can't drag a custom data object.
  • From: Billy Flatman <email@hidden>
  • Date: Tue, 27 Apr 2010 08:05:33 +0100

Hi Jens,

Thanks for your help. I've been trying to get the option 1 approach to work, but I don't know what I'm meant to do with my NSValue. I can't find how to convert it to an NSData type.

Thanks again,

Billy.

On 26 Apr 2010, at 15:46, Jens Alfke wrote:

>
> 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

Billy Flatman
email@hidden


_______________________________________________

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

  • Follow-Ups:
    • Re: Can't drag a custom data object.
      • From: Graham Cox <email@hidden>
References: 
 >Can't drag a custom data object. (From: Billy Flatman <email@hidden>)
 >Re: Can't drag a custom data object. (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: iPad Programming Tutorial
  • Next by Date: Re: Can't drag a custom data object.
  • Previous by thread: Re: Can't drag a custom data object.
  • Next by thread: Re: Can't drag a custom data object.
  • Index(es):
    • Date
    • Thread