gpt4 book ai didi

swift - 清除 UIPasteBoard

转载 作者:搜寻专家 更新时间:2023-11-01 06:19:40 26 4
gpt4 key购买 nike

假设在 UIPasteBoard 上复制了 3 个字符串:

UIPasteboard.generalPasteboard().string="hello"
UIPasteboard.generalPasteboard().string="world"
UIPasteboard.generalPasteboard().string="!"

我用 UIPasteboard.generalPasteboard().string=""

它会清除粘贴板吗? UIPasteBoard 是否有类似的功能,就像 NSPasteBoard 有 clearContents() 一样?

最佳答案

如果您知道您的程序是唯一一个操作特定粘贴板的程序,那么是的,将 string 属性设置为 "" 将有效地清除粘贴板。

您可以在 Playground 中轻松测试它

var pb = UIPasteboard.generalPasteboard()
pb.string = "hello"
pb.string
pb.items
pb.string = ""
pb.string
pb.items

输出

<UIPasteboard: 0x7fed6bd0a750>
<UIPasteboard: 0x7fed6bd0a750>
"hello"
[["public.utf8-plain-text": "hello"]]
<UIPasteboard: 0x7fed6bd0a750>
nil
[[:]]

但是,请注意 UIPasteboard 的 string 属性是第一个字符串类型的粘贴板项目的简写。可以通过 strings 属性访问所有字符串类型的项。

所有底层粘贴板项目都在 items 属性中建模,该属性是 [String: AnyObject] 类型的字典数组。每个字典在键中包含对象的类型信息,在值中包含粘贴板值。

因为您正在使用系统范围的 generalPasteboard,它也可以被其他程序操作,因此,要清除粘贴板中的所有项目,您应该使用

UIPasteboard.generalPasteboard().items = []

如果您将粘贴板用于内部应用程序,最好创建一个内部粘贴板,而不是使用系统范围的通用粘贴板。参见 pasteboardWithUniqueName()

关于swift - 清除 UIPasteBoard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36384506/

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