the Reverend
the Reverend

Reputation: 12559

Send an array of strings to NSPasteBoard

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

Answers (2)

the wolf
the wolf

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

Anne
Anne

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

Related Questions