3

我有一个属于此类的对象,其中包含以下声明:

标题

@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;

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

我希望能够将这种类型的对象复制和粘贴到/从NSPasteboard.

我有谷歌,但解释很模糊。

我应该怎么做才能使这个类对 NSPasteboard 可复制/可读,或者换句话说,使其符合NSPasteboardWriting协议NSPasteboardReading

我对这是如何完成的一无所知,而且像往常一样,Apple 文档和没有什么是一回事。

4

2 回答 2

11

这是完整的解决方案。

首先是使类NSCoding也符合。在这种情况下,类声明将是

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

为了使其与您兼容,NSCoding您必须实施encodeWithCoder:and 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
  }

要使该类与 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];
}

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

- (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.    
  }
}
于 2015-02-22T12:40:30.843 回答
0

只讨论写作。阅读基本相同(但当然相反;))

  1. 声明你写的类型

    - (NSArray *)writableTypesForPasteboard:(NSPasteboard *)pasteboard {
        return @[@"com.mycompany.mytype"];
    }
    
  2. 写入数据

    - (id)pasteboardPropertyListForType:(NSString *)type {
        //check the type we are asked to write
        if(![type isEqualToString:@"com.mycompany.mytype"]) return nil;    
    
        //create a _plist_ object
        // :: ONLY PLIST TYPES
        NSMutableDictionary *plist = [[NSMutableDictionary alloc] init];
    
        ...
    
        return plist;
    }
    
于 2015-02-22T10:19:42.180 回答