• 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 I make custom pasteboard type for an object reference?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can I make custom pasteboard type for an object reference?


  • Subject: Re: Can I make custom pasteboard type for an object reference?
  • From: Kiel Gillard <email@hidden>
  • Date: Thu, 15 Oct 2009 11:51:19 +1100

A few other alternatives come to mind:
- archive an identifier value that uniquely identifies the object you are dragging or
- use and archive an instance of NSValue using the valueWithPointer: initializer.


<http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSValue_Class/Reference/Reference.html#//apple_ref/occ/clm/NSValue/valueWithPointer: >

NSValue conforms to the NSCoding protocol, you should be able to use NSKeyedArchiver to archive an instance of NSValue, write the archive to the pasteboard and unarchive it at your dragging destination using NSKeyedUnarchiver.

Kiel

On 15/10/2009, at 11:35 AM, Rick Mann wrote:

That's the kind of thing I'm trying to avoid. There's no need to do that, since the drag is only within my app. I just want the drag receiver to have access to a *point* to the object, not a new copy of the object.

On Oct 14, 2009, at 17:34:06, Kiel Gillard wrote:

You could archive and unarchive your object as data using NSKeyedArchiver and NSKeyedUnarchiver.

<http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Archiving/Tasks/creating.html#//apple_ref/doc/uid/20000949 >
<http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Archiving/Tasks/codingobjects.html#//apple_ref/doc/uid/20000948 >


Kiel

On 15/10/2009, at 11:21 AM, Rick Mann wrote:

I'm trying to implement a library like Interface Builder's. When the user drags an item out of the library and onto one of my custom views, it should instantiate an object and place it in the view accordingly.

I'm trying to implement the drag by writing to the pasteboard an NSData object I create that contains a reference to the object, like so:

- (BOOL)
collectionView: (NSCollectionView*) inCollectionView
writeItemsAtIndexes: (NSIndexSet*) inIndices
toPasteboard: (NSPasteboard*) inPasteboard
{
MyObject* foo = self.myFoo;
if (foo != nil)
{
[inPasteboard declareTypes: [NSArray arrayWithObject: kUTIMyObjectRef] owner: nil];
[inPasteboard writeObjects: [NSArray arrayWithObject: plugIn]];


     return YES;
 }

 return NO;
}

In MyObject:

- (NSArray*)
writableTypesForPasteboard: (NSPasteboard*) inPasteboard
{
 static NSArray* types = nil;
 if (types == nil)
 {
     types = [NSArray arrayWithObjects: kUTIMyObjectRef, nil];
 }

 return types;
}

- (id)
pasteboardPropertyListForType: (NSString*) inType
{
 if ([inType isEqualToString: kUTIMyObjectRef])
 {
     NSMutableData* data = [NSMutableData data];
     [data appendBytes: &self length: sizeof (self)];
     return data;
 }

 return nil;
}

+ (NSArray*)
readableTypesForPasteboard: (NSPasteboard*) inPasteboard
{
 static NSArray* types = nil;
 if (types == nil)
 {
     types = [NSArray arrayWithObjects: kUTIMyObjectRef, nil];
 }

 return types;
}


But nowhere do I see a way to turn that NSData into an object reference, and I'm pretty sure I'm not implementing pasteboardPropertyListForType: correctly, anyway (I mimicked what I saw in the docs).


Am I just going about this all the wrong way?

TIA,
Rick

_______________________________________________

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



_______________________________________________

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


References: 
 >Can I make custom pasteboard type for an object reference? (From: Rick Mann <email@hidden>)
 >Re: Can I make custom pasteboard type for an object reference? (From: Kiel Gillard <email@hidden>)
 >Re: Can I make custom pasteboard type for an object reference? (From: Rick Mann <email@hidden>)

  • Prev by Date: Re: Can I make custom pasteboard type for an object reference?
  • Next by Date: Re: Can I make custom pasteboard type for an object reference?
  • Previous by thread: Re: Can I make custom pasteboard type for an object reference?
  • Next by thread: Re: Can I make custom pasteboard type for an object reference?
  • Index(es):
    • Date
    • Thread