Reputation: 12559
How would be the best way to send an array of strings or numbers to the pasteboard?
I've tried using [pasteBoard writeObjects:] but it looks like I have to subclass NSArray to do that, it asks for a protocol.
Maybe archiving and unarchiving or other ideas? Thank you, Jose.
Upvotes: 2
Views: 1144
Reputation: 35532
You can add a category to NSString to add strings to the pasteboard:
@implementation NSString (PasteboardGoodies)
- (void) sendToPasteboard
{
[[NSPasteboard generalPasteboard]
declareTypes: [NSArray arrayWithObject: NSStringPboardType]
owner:nil];
[[NSPasteboard generalPasteboard]
setString: self
forType: NSStringPboardType];
} // sendToPasteboard
@end // PasteboardGoodies
Upvotes: 1
Reputation: 27073
First convert the array to a string.
Next write it to the pasteboard.
NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"First Line",@"Second Line",nil];
NSPasteboard * pasteBoard = [NSPasteboard generalPasteboard];
NSString * string = [array componentsJoinedByString: @"\n"];
[pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[pasteBoard setString:string forType:NSStringPboardType];
Upvotes: 3