29

我无法让此方法返回 YES:

- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
    return [pasteBoard setString:stringToWrite forType:NSStringPboardType];
}

我已经验证 stringToWrite 正确通过,该方法总是返回 NO。

有任何想法吗?

这是课程的其余部分:

@interface ClipBoard : NSObject {
    NSPasteboard *pasteBoard;
}

- (BOOL) writeToPasteBoard:(NSString *)stringToWrite;
- (NSString *) readFromPasteBoard;
@end

@implementation ClipBoard
- (id) init
{
    [super init];
    pasteBoard = [NSPasteboard generalPasteboard];
    return self;
}

- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
    return [pasteBoard setString:stringToWrite forType:NSStringPboardType];
}

- (NSString *) readFromPasteBoard
{
    return [pasteBoard stringForType:NSStringPboardType];
}

@结尾

4

8 回答 8

37

这是该方法的工作版本:

- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
    [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
    return [pasteBoard setString:stringToWrite forType:NSStringPboardType];
}
于 2009-02-28T20:26:04.067 回答
21

斯威夫特 2:

使用 Swift 2 将字符串复制到通用粘贴板:

let pasteboard = NSPasteboard.generalPasteboard()
pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
pasteboard.setString("Hello", forType: NSPasteboardTypeString)
于 2016-01-20T14:37:45.877 回答
15

Apple 建议人们远离 NSStringPboardType 并改用 NSPasteboardTypeString。

于 2012-03-21T08:53:41.203 回答
12

NSPasteboard上复制字符串之前,最好清除内容然后保存。

斯威夫特 4

    // Set string
    NSPasteboard.general.clearContents()
    NSPasteboard.general.setString("I copied a string", forType: .string)
    // Read copied string
    NSPasteboard.general.string(forType: .string)

Objective-C

    // Set string
    [[NSPasteboard generalPasteboard] clearContents];
    [[NSPasteboard generalPasteboard] setString:@"I copied a string" forType:NSPasteboardTypeString];
    // Read string
    [[NSPasteboard generalPasteboard] stringForType:NSPasteboardTypeString];

还有其他可用类型可用于在NSPasteboard上复制项目。像:

  • NSPasteboardTypeString
  • NSPasteboardTypePDF
  • NSPasteboardTypeTIFF
  • NSPasteboardTypePNG
  • NSPasteboardTypeRTF

您可以在https://developer.apple.com/documentation/appkit/nspasteboardtype上找到更多详细信息。

于 2018-02-14T13:08:40.510 回答
11

从 10.6 开始,以下实现也是可能的:

- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
    [pasteBoard clearContents];
    return [pasteBoard writeObjects:[NSArray arrayWithObject:stringToWrite]];
}

重要的是要注意在将新内容写入粘贴板之前必须调用#clearContents,否则#writeObjects: 会一直返回NO。

新的 #writeObjects: 方法可以用于符合 NSPasteboardWriting 协议的对象。还有一个 NSPasteboardReading 协议,但使用它不会使实现变得更简单。

于 2012-04-10T12:02:24.553 回答
3

斯威夫特 4 版本:

NSPasteboard.general.clearContents()
NSPasteboard.general.setString("Hello World", forType: .string)
于 2017-10-17T09:10:14.780 回答
1

这适用于 Mojave 10.14.5,并且不使用任何已弃用的 API:

NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString, nil] owner:nil];
[pasteboard setString:@"Hello clipboard!" forType:NSPasteboardTypeString];
于 2019-07-25T10:47:07.860 回答
0

为了记录,要将字符串复制到系统剪贴板,您可以使用

NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
[pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[pasteBoard setString:string forType:NSStringPboardType];
于 2015-09-10T18:50:16.810 回答