gpt4 book ai didi

macos - 无法写入普通粘贴板

转载 作者:行者123 更新时间:2023-12-03 16:36:04 25 4
gpt4 key购买 nike

我有一个自定义 NSView,它拖动自定义核心数据对象。我通过调用此方法开始拖动过程​​:

[self dragImage:graphicItem.dragImage at:point offset:CGSizeZero event:theEvent pasteboard:pb source:self slideBack:YES];

在调用上面的方法之前,我抓取通用粘贴板并设置如下标题:

pb = [NSPasteboard generalPastebaord];
[pb setString:graphicItem.itemDescription forType:(NSString *)kUTTypeText];

但在我设置字符串后立即调用:

[pb stringForType:(NSString *)kUTTypeText]

我什么也没得到。

如果我创建一个像这样的粘贴板:

pb = [NSPasteboard pasteboardWithName:"myPasteBoard"]

它确实有效。所以,我尝试使用另一个标准粘贴板

pb = [NSPasteboard pasteboardWithName: NSDragPboard];

但是这个也不允许我向其写入数据。

我从 mouseDragged:(NSEvent *)event; 调用此方法我的应用程序是沙盒的。有什么想法吗?

最佳答案

使用普通粘贴板时,必须清除其中的内容,为新项目做好准备。
来自 docs :

Clears the existing contents of the pasteboard, preparing it for new contents. This is the first step in providing data on the pasteboard.

Apple 还为纯文本字符串粘贴板内容提供了一个字符串常量:NSPasteboardTypeString

以下代码有效,并使用通用粘贴板:

NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard setString:@"TestString" forType:NSPasteboardTypeString];
NSLog(@"%@", [pasteboard stringForType:NSPasteboardTypeString]);

关于macos - 无法写入普通粘贴板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23117865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com