Search code examples
objective-ccopyclonecopy-constructor

Arguments for a copy method versus a copy constructor in Objective-C


I'm relatively new to the world of Objective-C and have a class that I've written to which I'd like to add the ability to create a copy.

Coming from Java, I know that I could either write a copy constructor or a clone method, with the latter commonly not being recommended. However, Objective-C is a different language and before I proceed I'd like to understand the arguments for a copy method versus a copy constructor in Objective-C. What is the most commonly used approach?


Solution

  • The recommended way to add the ability to copy objects is by implementing the NSCopying protocol. Many foundation classes also implement NSCopying.

    Refer to this answer for more information: Implementing NSCopying