问题描述
您可以像这样调用 NSPasteboard
:
[pboard declareTypes :types owner:self];
这意味着粘贴板稍后会要求所有者根据需要提供类型的数据。但是,我不能从文档找到(也许我错过了流血明显),是否所有者
被保留。
在实践中,令人担心的是,如果所有者是弱引用,它可能会被释放,导致崩溃,如果粘贴板尝试从它请求数据。 / p>
注意:我应该澄清一下,我对此感兴趣,作为跟踪一个错误的帮助,而不是让我的应用程序依赖它。
blockquote>
newOwner
负责将
数据写入粘贴板的对象,
立即提供所有类型的数据
。如果指定一个newOwner
对象,它必须支持在newTypes
参数中声明的所有
类型,并且必须保持有效为
,因为数据承诺在
粘贴板。
翻译:粘贴板可能保留或不保留所有者。它是否是一个实现细节,你不应该依赖。
文档对保持有效的说法实际上指的是您可能会懒惰地提供的代理内容。也就是说如果用户要复制某些内容,则不希望所有者对所复制内容的表示发生更改,因为用户进行进一步编辑时会出现稍后粘贴的意图。
文档没有说明所有者的保留/释放策略(也没有任何一种总括规则语句)。应该澄清(rdar:// 8966209提交)。事实上,假设保留/释放行为是危险的。
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.
The docs:
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.
这篇关于NSPasteboard是否保留所有者对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!