Cocoa Programming for Mac OS X - Aaron Hillegass [91]
Prior to Mac OS X 10.6, the pasteboard APIs allowed for only one item on the pasteboard at any one time (although that item could have an arbitrary number of representations, or types). In 10.6, Apple updated the pasteboard APIs to allow for multiple items on a pasteboard: for example, multiple URLs copied from Finder. Each item can have multiple types associated with it, such as string and URL representations.
When putting data on the pasteboard, your application typically clears the pasteboard and then writes one or more objects directly to the pasteboard. Each of those objects forms an individual item on the pasteboard. The objects must conform to a pasteboard-writing protocol, which supplies the data. In this case the data for those items is immediately copied to the pasteboard.
The receiving application will then ask the pasteboard for an array of objects. It supplies an array of classes along with this request, which enables the pasteboard to provide the richest representations available for each item. In this scenario, the classes must conform to the pasteboard-reading protocol.
Data can also be passed via the pasteboard lazily. To do so, a class declares the data it will provide and then promises to provide that data when asked to do so by means of a delegate method in the future. We will talk about lazy copying at the end of the chapter.
Apple also provides APIs to work with the pasteboard on a per type basis, which may be useful if your application requires very fine control of the pasteboard. These APIs match the pre-10.6 APIs very closely.
Multiple pasteboards are available. There is a pasteboard called the general pasteboard, for copy-and-paste operations and another for drag-and-drop tasks. One pasteboard stores the last string that the user searched for, another copies rulers, and another copies fonts.
In this chapter, you will add cut, copy, and paste capabilities to your BigLetterView. First, you will implement the methods that will read from and write to the pasteboard. Then we will discuss how those methods get called.
NSPasteboard
As mentioned earlier, the NSPasteboard class acts as an interface to the pasteboard server. Following are some of the commonly used methods of NSPasteboard:
+ (NSPasteboard *)generalPasteboard
Returns the general NSPasteboard. You will use this pasteboard to copy, cut, and paste.
+ (NSPasteboard *)pasteboardWithName:(NSString *)name
Returns the pasteboard identified by name. Here are the global variables that contain the names of the standard pasteboards:
NSGeneralPboard
NSFontPboard
NSRulerPboard
NSFindPboard
NSDragPboard
- (NSInteger)clearContents
Clears the contents of the pasteboard before writing objects to it. Returns the current change count of this pasteboard, which is not needed in most applications.
- (BOOL)writeObjects:(NSArray *)objects
Writes to the pasteboard objects that conform to the NSPasteboardWriting protocol. Conforming classes include NSString, NSAttributedString, NSURL, and NSImage. Each object represents an individual pasteboard item. If multiple types are to be written for each item, the object must write those types through the pasteboard-writing protocol.
For example, if an array of NSURL objects is written to the pasteboard, a pasteboard item will be created for each NSURL. Each of those pasteboard items will have two types associated with it: public.url and public. utf8-plain-text.
- (NSArray *)readObjectsForClasses:(NSArray *)classes
options:(NSDictionary *)options
Reads objects from the pasteboard. One object will be returned per pasteboard item. An array of classes must be passed that describe the order in which the types are desired, usually with the richest representation first. The classes must conform to the NSPasteboardReading protocol.
Following on the previous example, if the classes array contains only [NSURL class], an array of NSURL instances will be returned. If it contains only [NSString class], NSString