2011-03-15 77 views
1

將一串字符串或數字發送到粘貼板的最佳方式是什麼?向NSPasteBoard發送一串字符串

我試過使用[pasteBoard writeObjects:]但它看起來像我必須繼承NSArray來做到這一點,它要求一個協議。

也許歸檔和取消歸檔或其他想法?謝謝

回答

2

首先數組轉換爲字符串。
接下來將它寫入粘貼板。

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

您可以將類別添加到的NSString字符串添加到剪貼板:

@implementation NSString (PasteboardGoodies) 
- (void) sendToPasteboard 
{ 
     [[NSPasteboard generalPasteboard] 
      declareTypes: [NSArray arrayWithObject: NSStringPboardType] 
      owner:nil]; 
     [[NSPasteboard generalPasteboard] 
      setString: self 
      forType: NSStringPboardType]; 
} // sendToPasteboard 

@end // PasteboardGoodies