NSPasteboard 是否保留所有者对象?

发布于 2024-10-16 08:21:37 字数 344 浏览 4 评论 0原文

您可以像这样调用 NSPasteboard

[pboard declareTypes:types owner:self];

这意味着粘贴板稍后会要求所有者根据需要提供类型的数据。然而,我无法从文档中找到(也许我错过了一些明显的东西),是否保留 owner

在实践中,让我担心的是,如果所有者是弱引用,它可能会被释放,如果粘贴板尝试向其请求数据,则会导致崩溃。

注意:我可能应该澄清一下,我对此更感兴趣,因为它有助于追踪错误,而不是让我的应用程序依赖它。但我确实也希望文档得到澄清。

You can call NSPasteboard like this:

[pboard declareTypes:types owner:self];

Which means that the pasteboard will later ask the owner to supply data for a type as needed. However, what I can't find from the docs (and maybe I've missed something bleeding obvious), is whether or not owner is retained.

In practice what's worrying me is if the owner is a weak reference, it could be deallocated, causing a crash if the pasteboard then tries to request data from it.

Note: I should probably clarify that I'm interested in this more as an aid to tracking down a bug, than making my app rely on it. But I do also want the docs clarified.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木緿 2024-10-23 08:21:37

文档:

新所有者

负责写入的对象
数据到粘贴板,或者为零,如果你
提供所有类型的数据
立即地。如果您指定一个 newOwner
对象,它必须支持所有
newTypes 中声明的类型
参数并且必须保持有效
只要数据上承诺
纸板。

翻译:粘贴板可能会也可能不会保留所有者。是否确实如此是您不应该依赖的实现细节。只要所有者仍担任所有者,您就有责任保留所有者。

文档所说的“保持有效”实际上是指您可能懒惰提供的代理内容。即,如果用户要复制某些内容,您不希望当用户进行进一步编辑并打算稍后粘贴时,所有者对复制内容的表示会发生变化。

该文档没有提及所有者的保留/释放策略(也没有任何类型的一揽子规则声明)。应该澄清(rdar://8966209 提交)。事实上,对保留/释放行为做出假设是危险的。

The docs:

newOwner

The object responsible for writing
data to the pasteboard, or nil if you
provide data for all types
immediately. If you specify a newOwner
object, it must support all of the
types declared in the newTypes
parameter and must remain valid for as
long as the data is promised on the
pasteboard.

Translation: The pasteboard may or may not retain the owner. Whether it does is an implementation detail that you should not rely upon. It is your responsibility to retain the owner for as long as it acts as an owner.

What the docs are saying about "remain valid" actually refers to the proxied contents that you might lazily provide. I.e. if the user were to copy something, you wouldn't want the owner's representation of what was copied to change as the user makes further edits with an intention of pasting sometime later.

The documentation says nothing about the retain/release policy of the owner (nor is there any kind of blanket rule statement). It should be clarified (rdar://8966209 filed). As it is, making an assumption about the retain/release behavior is dangerous.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文