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

时间:2015-02-22 09:58:20

标签: macos cocoa nspasteboard

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

标题

@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包含可以是NSViewNSImageView的对象。

我希望能够将此类型的对象复制并粘贴到NSPasteboard

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

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

我不知道如何做到这一点,像往常一样,Apple文档并没有什么是同一件事。

2 个答案:

答案 0 :(得分:7)

这是完整的解决方案。

第一件事是使类符合NSCoding。在这种情况下,类声明将是

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

要使其与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
  }

要使该类适用于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.    
  }
}

答案 1 :(得分: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;
    }