xcode 将字符串写入 NSPasteBoard
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/598587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a string to NSPasteBoard
提问by macinjosh
I cannot get this method to return YES:
我无法让此方法返回 YES:
- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
return [pasteBoard setString:stringToWrite forType:NSStringPboardType];
}
I have verified that stringToWrite is coming through properly, the method just always returns NO.
我已经验证 stringToWrite 正确通过,该方法总是返回 NO。
Any ideas?
有任何想法吗?
Here is the rest of the class:
这是课程的其余部分:
@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];
}
@end
@结尾
回答by macinjosh
Here is the working version of the method:
这是该方法的工作版本:
- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
[pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
return [pasteBoard setString:stringToWrite forType:NSStringPboardType];
}
回答by Sebastian
Swift 2:
斯威夫特 2:
Copy a string to the general pasteboard with Swift 2:
使用 Swift 2 将字符串复制到通用粘贴板:
let pasteboard = NSPasteboard.generalPasteboard()
pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
pasteboard.setString("Hello", forType: NSPasteboardTypeString)
回答by Jason Fuerstenberg
Apple is suggesting people move away from NSStringPboardType and use NSPasteboardTypeString instead.
Apple 建议人们远离 NSStringPboardType 并改用 NSPasteboardTypeString。
回答by Karsten
As of 10.6, the following implementation is also possible:
从 10.6 开始,以下实现也是可能的:
- (BOOL) writeToPasteBoard:(NSString *)stringToWrite
{
[pasteBoard clearContents];
return [pasteBoard writeObjects:[NSArray arrayWithObject:stringToWrite]];
}
It's important to notice that #clearContents has to be called before something new can be written to the pasteboard, otherwise #writeObjects: keeps returning NO.
重要的是要注意在将新内容写入粘贴板之前必须调用 #clearContents,否则 #writeObjects: 会一直返回 NO。
The new #writeObjects: methods is possible for objects that conform to the NSPasteboardWriting protocol. There's also an NSPasteboardReading Protocol, but using it wouldn't make the implementation any simpler.
新的#writeObjects: 方法适用于符合 NSPasteboardWriting 协议的对象。还有一个 NSPasteboardReading 协议,但使用它不会使实现更简单。
回答by abdullahselek
Before you copy a string on NSPasteboardit's better to clear the contents and then save.
在NSPasteboard上复制字符串之前,最好先清除内容然后保存。
Swift 4
斯威夫特 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
目标-C
// Set string
[[NSPasteboard generalPasteboard] clearContents];
[[NSPasteboard generalPasteboard] setString:@"I copied a string" forType:NSPasteboardTypeString];
// Read string
[[NSPasteboard generalPasteboard] stringForType:NSPasteboardTypeString];
And also there are other available types for copying items on NSPasteboard. Like:
并且还有其他可用类型可用于在NSPasteboard上复制项目。喜欢:
- NSPasteboardTypeString
- NSPasteboardTypePDF
- NSPasteboardTypeTIFF
- NSPasteboardTypePNG
- NSPasteboardTypeRTF
- NSPasteboardTypeString
- NSPasteboardTypePDF
- NSPasteboardTypeTIFF
- NSPasteboardTypePNG
- NSPasteboardTypeRTF
You can find more details on https://developer.apple.com/documentation/appkit/nspasteboardtype.
您可以在https://developer.apple.com/documentation/appkit/nspasteboardtype上找到更多详细信息。
回答by Dave Wood
Swift 4 version:
斯威夫特 4 版本:
NSPasteboard.general.clearContents()
NSPasteboard.general.setString("Hello World", forType: .string)
回答by mrexodia
This works on Mojave 10.14.5 and does not use any deprecated APIs:
这适用于 Mojave 10.14.5,并且不使用任何已弃用的 API:
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString, nil] owner:nil];
[pasteboard setString:@"Hello clipboard!" forType:NSPasteboardTypeString];
回答by Snowman
For the record, to copy a string to the system clipboard, you can use
为了记录,要将字符串复制到系统剪贴板,您可以使用
NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
[pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[pasteBoard setString:string forType:NSStringPboardType];