简体   繁体   English

为什么要对具有自定义内存管理语义的集合使用免费电话桥接?

[英]Why use toll-free bridging for collections with custom memory-management semantics?

In the book 在书里

Effective Objective-C 2.0: 52 Specific Ways to Improve Your iOS and OS X Program 有效的Objective-C 2.0:52种改善iOS和OS X程序的特定方法

there is an 有一个

Item 49: Use Toll-Free Bridging for Collections with Custom Memory-Management Semantics 条款49:对具有自定义内存管理语义的集合使用免费电话桥接

This chapter mainly explains the importance of toll-free bridging with an example of solving the problem that keys are copied in NSDictionary while values are retained. 本章主要通过举例说明免费密钥桥接的重要性,该示例解决了在保留值的同时将密钥复制到NSDictionary中的问题。

It says and I quote as following: 它说,我引用如下:

What if the objects you want to use as keys cannot be copied? 如果无法复制要用作键的对象怎么办?

My question is that if all objects in Objective-C are subclasses of NSObject which implements copy methods from the very beginning, how can there be objects that cannot be copied? 我的问题是,如果Objective-C中的所有对象都是从一开始就实现复制方法的NSObject的子类,那么怎么会有无法复制的对象呢? Are there any real scenarios that call for this situation? 是否有任何实际情况需要这种情况?

NSObject implements -copy as a convenience in terms of NSCopying 's -copyWithZone: . NSObject实现-copy作为方面便利NSCopying-copyWithZone: If your object doesn't conform to NSCopying and implement -copyWithZone: , calling -copy on it will throw an exception. 如果你的对象不符合NSCopying和实施-copyWithZone:呼吁-copy它会抛出异常。 It's not true that all objects implement copying, which is why you would need to retain them instead of copying. 不是所有对象都实现复制,这就是为什么您需要保留它们而不是复制的原因。 (See the documentation for -[NSObject copy] for more information on -copy .) (有关-copy更多信息,请参见-[NSObject copy]文档 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM