我很难在我的自定义托管对象Person
上进行复制/粘贴。该对象包含属性和关系。该对象应该提供objectID。我打算实现粘贴生成一个新对象,然后填写复制的Person
中的信息。
复制objectID可能会起作用。我确信粘贴不起作用。我在Person
类中实现了以下方法,试图复制/粘贴对象:
#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;
}
并做贴纸:
#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;
}
我该怎么办?我很茫然,阅读许多stackoverflow Q& A(例如Peter Hosey对NSPasteboard and simple custom data的好回答),以及Apple文档,仍然让我难以理解。
答案 0 :(得分:0)
您是否已经看过这个: Using Managed Objects?
我不确定您的意思'我需要返回Person
个对象,但我没有managed object context
。你的上下文是你创造事物的便利贴。