0

我有一个字典,其中包含NSData格式的图像、图像的大小以及图像的其他属性。我怎样才能NSDictionary写到NSPasteboard

我写的代码如下:

NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
[pasteBoard clearContents];
[pasteBoard writeObjects:[NSArray arrayWithObject:myDictionary];

编译后,它会在控制台中抛出以下消息:

class __NSDictionaryM对无效的实例NSPasteboard -writeObjects:class __NSDictionaryM不实现NSPasteboardWriting协议。

4

2 回答 2

2

The objects should implement the protocol NSPasteboardWriting to be saved in the pasteBoard server.

The Cocoa framework classes NSString, NSAttributedString, NSURL, NSColor, NSSound, NSImage, and NSPasteboardItem implement this protocol. You can make your custom class conform to this protocol so that you can write instances of the class to a pasteboard using the writeObjects: method of NSPasteboard.

why not doing like this :

NSImage *image = ...;
NSArray *array = [NSArray arrayWithObject:image];
NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
[pasteBoard writeObjects:array];
于 2013-10-03T08:07:02.270 回答
0

要么用这些数据创建你自己的类并实现NSPasteboardWriting(可能NSPasteboardReading也是因为你也想从你的自定义粘贴板格式中读取这些数据,因为没有其他应用程序会理解它),或者使用setPropertyList:forType:and propertyList:forType:.

于 2013-10-03T08:43:53.413 回答