文档原文,实现的规范
- Your options for implementing this protocol are as follows:
- Implement NSCopying using alloc and init... in classes that don’t inherit copyWithZone:.
- Implement NSCopying by invoking the superclass’s copyWithZone: when NSCopying behavior is inherited. If the superclass implementation might use the NSCopyObject function, make explicit assignments to pointer instance variables for retained objects.
- Implement NSCopying by retaining the original instead of creating a new copy when the class and its contents are immutable.
1.如果这个类不是从一个已经实现NSCopying协议的类继承而来那么它应该使用
[[ alloc]init]创建对象,初始化后返回。
2.如果它是从一个已经实现NSCopying协议的类继承来,那么它应该调用[super copyWithZone]方法。如果父类使用NSCopyObject方法,那么直接创建一个引用接收它。
3.如果父类已经实现NSCopying协议,且数据是不可变长的,那么直接创建一个引用接收它,不要再创建一个新的对象了。
总结:
其实这些原则从很大程序上来讲是为了避免内存泄漏,和提高效率降低出错率。
引用一下一个例子:
|
Code:
Of course, whether you copy the ivars, retain them, or just assign them should mirror what the setters do. |