A protocol that objects adopt to provide functional copies of themselves.
- iOS 2.0+
- macOS 10.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
The exact meaning of “copy” can vary from class to class, but a copy must be a functionally independent object with values identical to the original at the time the copy was made. A copy produced with
NSCopying is implicitly retained by the sender, who is responsible for releasing it.
NSCopying declares one method,
copy, but copying is commonly invoked with the convenience method
copy method is defined for all objects inheriting from
NSObject and simply invokes
copy with the default zone.
Your options for implementing this protocol are as follows:
NSCopyingby invoking the superclass’s
NSCopyingbehavior is inherited. If the superclass implementation might use the
NSCopyfunction, make explicit assignments to pointer instance variables for retained objects.
NSCopyingby retaining the original instead of creating a new copy when the class and its contents are immutable.
If a subclass inherits
NSCopying from its superclass and declares additional instance variables, the subclass has to override
copy to properly handle its own instance variables, invoking the superclass’s implementation first.