Send an array of strings to NSPasteBoard
-
24-10-2019 - |
Question
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.
Solution
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];
OTHER TIPS
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow