答案 0 :(得分:11)
你应该能够将它们转换为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
}
答案 1 :(得分:4)
您可以在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!;
}
答案 2 :(得分:0)
对于 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
}