1

I am having a very hard time getting copy/paste to work on my custom managed object Person. The object contains properties and relationships. The object should provide the objectID. I intend to implement pasting generating a new object and then filling in the information from the copied Person.

Copying the objectID probably does work. I am certain that pasting does not work. I have the following methods implemented in my Person class, in an attempt to copy/paste an object:

#pragma mark --- Copy functionality

-(id)pasteboardPropertyListForType:(NSString *)type
{
    if ( [type isEqualToString:@"my.company.person"])
    {
        NSManagedObjectID *oid = self.objectID;
        NSURL *uidURL = [oid URIRepresentation];
        return [uidURL absoluteString];
    }
    return nil;
}

-(NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard
{
    return @[@"my.company.person"];
}

+ (NSPasteboardWritingOptions)writingOptionsForType:(NSString *)type pasteboard:(NSPasteboard *)pasteboard
{
    if ( [type isEqualToString:@"my.company.person"])
    {
        return NSPasteboardWritingPromised;
    }
    return nil;
}

and to do the pasting:

#pragma mark --- Paste functionality

+(NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard
{
    return @[@"my.company.person"];
}

+ (NSPasteboardReadingOptions)readingOptionsForType:(NSString *)type pasteboard:(NSPasteboard *)pasteboard
{
    if ( [type isEqualToString:@"my.company.person"])
    {
        return NSPasteboardReadingAsString;
    }
    return nil;
}

- (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type
{
    if ( [type isEqualToString:@"my.company.person"])
    {
        ...
    }
    return nil;
}

How should I proceed here? I am at a loss, reading many stackoverflow Q&A's (e.g. Peter Hosey's great answer to NSPasteboard and simple custom data), as well as the Apple docs, still have me stumped on this one.

Community
  • 1
  • 1
Carelinkz
  • 936
  • 8
  • 27
  • I already found one problem with my code; I should have `readingOptionsForType:` set at NSPasteboardReadingAsString. Still, I am wondering if this is the cleanest way of handling the "simple" copying of an objectID. – Carelinkz Apr 08 '13 at 13:52
  • Specific problem: the above approach can get me an ObjectID in `initWithPasteboardPropertyList:` but I need to return a Person object. Unfortunately I do not have a managed object context yet. What gives? – Carelinkz Apr 08 '13 at 14:43
  • did you ever get a solution to rehydrating your managed object from the Pasteboard? – Brad M Dec 05 '13 at 03:10
  • I kind of did, but not as elegantly as I wanted. I now use a document controller which receives a notification from, for example, the `Person` object, that contains the objectID. The controller has access to the managed object context, and orchestrates whatever needs to be done. Not what I had wanted but it does do the job. – Carelinkz Dec 05 '13 at 17:32

1 Answers1

0

Have you already seen this: Using Managed Objects?

I'm not sure what you mean about 'I need to return a Person object but I don't have a managed object context.' Your context is your scratch pad to create things.

  • I have, thanks. The managed object context is not available yet, because (I guess) this `Person` object is being created through the NSPasteboardReading protocol. It is not initialized yet. Every other `Person` does indeed have a managed object context. – Carelinkz Apr 09 '13 at 12:56