从NSPasteBoard存储和检索自定义对象

Spa*_*Dog 3 macos cocoa nspasteboard

我有一个属于这个类的对象,它包含以下声明:

HEADER

@interface MyClassObject : NSObject <NSCopying, NSPasteboardWriting, NSPasteboardReading>

@property (nonatomic, strong) NSArray *children;
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) id node;
Run Code Online (Sandbox Code Playgroud)

children保存此类的其他对象,node保存可以是NSView或的对象NSImageView.

我希望能够复制和粘贴此类型的对象NSPasteboard.

我有谷歌,但解释是模糊的.

我应该怎么做才能使这个类可以复制/读取NSPasteboard,换句话说,使它符合NSPasteboardWritingNSPasteboardReading协议?

关于如何做到这一点,我没有丝毫的线索,像往常一样,Apple的文档并没有什么是同一件事.

Spa*_*Dog 8

这是完整的解决方案.

首先是使课程符合要求NSCoding.在这种情况下,类声明将是

@interface MyClassObject : NSObject <NSCopying, NSPasteboardWriting, NSPasteboardReading, NSCoding>
Run Code Online (Sandbox Code Playgroud)

为了使它与NSCoding你兼容必须实现encodeWithCoder:initWithCoder:...在我的情况下:

- (void)encodeWithCoder:(NSCoder *)coder {

  [coder encodeObject:@(self.type) forKey:@"type"];
  [coder encodeObject:self.name forKey:@"name"];
  // converting the node to NSData... 
  // the object contained in node must be compatible with NSCoding
  NSData *nodeData = [NSKeyedArchiver archivedDataWithRootObject:self.node];
  [coder encodeObject:nodeData forKey:@"node"];

  NSData *childrenData = [NSKeyedArchiver archivedDataWithRootObject:self.children];
  [coder encodeObject:childrenData forKey:@"children"];

}

- (id)initWithCoder:(NSCoder *)coder {

  self = [super init];

  if (self) {

    _type = [[coder decodeObjectForKey:@"type"] integerValue];
    _name = [coder decodeObjectForKey:@"name"];

    NSData *nodeData = [coder decodeObjectForKey:@"node"];
    _efeito = [NSKeyedUnarchiver unarchiveObjectWithData:nodeData];

    NSData *childrenData = [coder decodeObjectForKey:@"children"];
    _children = [NSKeyedUnarchiver unarchiveObjectWithData:childrenData];
    _parent = nil;  // I want this to be nil when the object is recreated
  }
Run Code Online (Sandbox Code Playgroud)

要使该类与NSPasteboard一起使用,您必须添加以下4种方法:

-(id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type {
  return [NSKeyedUnarchiver unarchiveObjectWithData:propertyList];
}


+(NSArray *)readableTypesForPasteboard:(NSPasteboard *)pasteboard {
    // I am using the bundleID as a type  
    return @[[[NSBundle mainBundle] bundleIdentifier]];
}

- (NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard {
  // I am using the bundleID as a type  
  return @[[[NSBundle mainBundle] bundleIdentifier]];
}


- (id)pasteboardPropertyListForType:(NSString *)type {
  // I am using the bundleID as a type  
  if(![type isEqualToString:[[NSBundle mainBundle] bundleIdentifier]]) {
    return nil;
  }

  return [NSKeyedArchiver archivedDataWithRootObject:self];
}
Run Code Online (Sandbox Code Playgroud)

然后,在您实现复制和粘贴的类上添加:

- (void)copy:(id)sender {

  NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
  [pasteBoard clearContents];

  NSArray *copiedObjects = @[theObjectYouWantToCopyToThePasteboard];
  [pasteBoard writeObjects:copiedObjects];

}

- (void)paste:sender {

  NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
  NSArray *classArray = @[[LayerObject class]];
  NSDictionary *options = [NSDictionary dictionary];

  BOOL ok = [pasteboard canReadItemWithDataConformingToTypes:@[[[NSBundle mainBundle] bundleIdentifier]]];

  if (ok) {
    NSArray *objectsToPaste = [pasteboard readObjectsForClasses:classArray options:options];
    MyObjectClass *object = [objectsToPaste objectAtIndex:0];

    // object is the one you have copied to the pasteboard
    // now you can do whatever you want with it.
    // add the code here.    
  }
}
Run Code Online (Sandbox Code Playgroud)