你应该只需要将它们转换为Swift:
@IBAction func copy() {
let pb: UIPasteboard = UIPasteboard.generalPasteboard();
pb.string = textView.text // Or another source of text
}
@IBAction func paste() {
let pb: UIPasteboard = UIPasteboard.generalPasteboard();
textView.text /*(Or somewhere to put the text)*/ = pb.string
}
您可以在Swift中实现复制和粘贴方法,如下所示:
// Function receives the text as argument for copying
func copyText(textToCopy : NSString)
{
let pasteBoard = UIPasteboard.generalPasteboard();
pasteBoard.string = textToCopy; // Set your text here
}
// Function returns the copied string
func pasteText() -> NSString
{
let pasteBoard = UIPasteboard.generalPasteboard();
println("Copied Text : \(pasteBoard.string)"); // It prints the copied text
return pasteBoard.string!;
}
对于Swift 5.0及以上版本
func copyText(from text: String) {
weak var pb: UIPasteboard? = .general
pb?.string = text
}
func pasteText() -> String? {
weak var pb: UIPasteboard? = .general
guard let text = pb?.string else { return nil}
return text
}
添加任何标准的Swift类型的简单方法是使用setter:
UIPasteboard.general.string = "Pasted String"
let pb: UIPasteboard = UIPasteboard.general
可以翻译为:let pb: UIPasteboard = UIPasteboard.general
。 - spacecash21