我可以使用 [NSObject hash] 将 NSView 存储在字典中吗?
我读到 -hash 在不同的体系结构上不会返回相同的值。但是,我目前在文档中没有看到这一点。
有没有更好的方法将 NSView 存储在字典中而无需子类化?
I've read that -hash does not return the same value on different architectures. However, I don't currently see this in the docs.
Is there a better way to store NSView's in a dictionary without subclassing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会对哈希在这种情况下的含义感到困惑。哈希值只是 Cocoa 集合类在内部使用的一个数字,用于提高性能。 NSObject 实现了此方法,因此您不必重写它,除非更好、更有意义的哈希算法在比较/搜索数组或字典中的对象时可以带来更好的性能。
由于任何对象都不应在磁盘上缓存其哈希值,因此该注释仅意味着您不应依赖 Apple 类返回的具体哈希值。它被认为是一个次要的实现细节。
NSDictionary 保留其值但复制键。因此,您无需执行任何操作即可将视图作为值保留在字典中,但如果您想将视图用作键,则必须实现 -copyWithZone: 方法。
请阅读 Apple 文档了解更多信息。
You might be confused as to what hash means in this context. Hash is just a number which Cocoa collection classes use internally to improve performance. NSObject implements this method, so you don't have to ever override it unless a better, more meaningful hash algorithm results in better performance while comparing/searching objects in an array or a dictionary.
Since no objects should cache their hash values on disk, the comment just implies you should not rely on the concrete hash values returned by Apple's classes. It is considered a minor implementation detail.
NSDictionary retains its values but copies the keys. So you don't have to do anything to keep your views in a dictionary as values, but if you want to use the views as keys, you have to implement -copyWithZone: method.
Read more in Apple's documentation.