0

I want to convert a custom object into a propertyList. Then later I want to retrieve it via NSOutlineView. I use a NSKeyedArchiver/unArchiver to convert my object to and from a NSData.

In my custom object (OutlineItem):

-(id)pasteboardPropertyListForType:(NSPasteboardType)type
{
if ([[[info draggingPasteboard] types] containsObject: kPrivateDragUTI])
{
     NSData *itemData = [NSKeyedArchiver archivedDataWithRootObject:self];
     NSString* itemString = [[NSString alloc]initWithData:itemData encoding:NSASCIIStringEncoding];
     return itemString;
}

In NSOutlineView:

- (id<NSPasteboardWriting>)outlineView:(NSOutlineView *)outlineView pasteboardWriterForItem:(id)item
{
   OutlineItem* anItem = item;
   return anItem;
}

Later:

- (BOOL)outlineView:(NSOutlineView *)destinationOutlineView
             acceptDrop:(id <NSDraggingInfo>)info item:(id)destinationParentItem
             childIndex:(NSInteger)destinationIndex
{    
    NSPasteboard *pboard = [info draggingPasteboard];
    OutlineItem* pbItem = nil;
    if ([[pboard types] containsObject: kPrivateDragUTI])
    {
        NSString* stringForData = [[info draggingPasteboard] stringForType:kPrivateDragUTI];
        NSData *data = [stringForData dataUsingEncoding:NSASCIIStringEncoding];

StringForData looks OK, but data is nil at this point.

Why is data nil?

Willeke
  • 14,578
  • 4
  • 19
  • 47
Carl Carlson
  • 500
  • 4
  • 17
  • What is the value of `stringForData`? Are all characters in the string ASCII characters? Do you need the string, can't you put the data on the pasteboard? – Willeke Mar 27 '21 at 07:36
  • stringForData begins like this: @"bplist00Ô\0\x01\0\x02\0\x03\0\x04\0\x05\0\x06\0\a\0\nX$versionY$archiverT$topX$objects\x12\0\x01\U00000086 _\x10\x0fNSKeyedArchiverÑ\0\b\0\tTroot\U00000080\x01¯\x11\x03û\0\v\0\f\0\x17\0\x18\0\x1c\0#\0)\0-\01\08\09\0A\0B\0E\0H\0M\0S\0T\0a\0g\0h\0k\0o\0r\0v\0|\0}\0\U00000082\0\U00000088\0\U00000089\0\U0000008c\0\U00000090\0\U00000093... According to Apple's doc, NSData does not implement NSPasteboardWriting protocol – Carl Carlson Mar 27 '21 at 17:45
  • The characters in stringForData appear to be ASCII. – Carl Carlson Mar 27 '21 at 17:57
  • This post promises to do just this. I'll test it and report back. Until then, don't spend your time on this. https://stackoverflow.com/questions/6103579/nsdata-from-nskeyedarchiver-to-nsstring – Carl Carlson Mar 27 '21 at 19:25
  • The updated solution in this post works perfectly: https://stackoverflow.com/questions/6103579/nsdata-from-nskeyedarchiver-to-nsstring – Carl Carlson Mar 27 '21 at 19:39

0 Answers0