iOS Developer Library — Prerelease


Foundation Framework Reference NSCopying Protocol Reference

Deployment Target:

On This Page


The NSCopying protocol declares a method for providing functional copies of an object. 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, copyWithZone:, but copying is commonly invoked with the convenience method copy. The copy method is defined for all objects inheriting from NSObject and simply invokes copyWithZone: with the default zone.

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.

If a subclass inherits NSCopying from its superclass and declares additional instance variables, the subclass has to override copyWithZone: to properly handle its own instance variables, invoking the superclass’s implementation first.

  • Returns a new instance that’s a copy of the receiver.



    func copyWithZone(_ zone: NSZone) -> AnyObject


    - (id)copyWithZone:(NSZone *)zone



    This parameter is ignored. Memory zones are no longer used by Objective-C.


    The returned object is implicitly retained by the sender, who is responsible for releasing it. The copy returned is immutable if the consideration “immutable vs. mutable” applies to the receiving object; otherwise the exact nature of the copy is determined by the class.


    Available in iOS 2.0 and later.

    See Also

    – mutableCopyWithZone: (NSMutableCopying protocol)
    – copy (NSObject class)