N. Anant Vijay
N. Anant Vijay

Reputation: 43

Write NSDictionary to NSPasteboard

I have a dictionary containing an image in NSData format, the size of the image along with other attributes of image. How can i write an NSDictionary to NSPasteboard?

I wrote the code as following:

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

After compiling, It throws the following message in the console:

Instances of class __NSDictionaryM not valid for NSPasteboard -writeObjects:. The class __NSDictionaryM does not implement the NSPasteboardWriting protocol.

Upvotes: 0

Views: 1220

Answers (2)

Jesper
Jesper

Reputation: 7605

Either make your own class with this data and implement NSPasteboardWriting (and probably NSPasteboardReading too since you'll want to read this data out from your custom pasteboard format as well, since no other application will understand it), or use setPropertyList:forType: and propertyList:forType:.

Upvotes: 0

samir
samir

Reputation: 4551

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

Upvotes: 2

Related Questions