Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 375093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:28:09+00:00 2026-05-12T14:28:09+00:00

I need to deep copy a custom object that has objects of its own.

  • 0

I need to deep copy a custom object that has objects of its own. I’ve been reading around and am a bit confused as to how to inherit NSCopying and how to use NSCopyObject.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:28:09+00:00Added an answer on May 12, 2026 at 2:28 pm

    As always with reference types, there are two notions of “copy”. I’m sure you know them, but for completeness.

    1. A bitwise copy. In this, we just copy the memory bit for bit – this is what NSCopyObject does. Nearly always, it’s not what you want. Objects have internal state, other objects, etc, and often make assumptions that they’re the only ones holding references to that data. Bitwise copies break this assumption.
    2. A deep, logical copy. In this, we make a copy of the object, but without actually doing it bit by bit – we want an object that behaves the same for all intents and purposes, but isn’t (necessarily) a memory-identical clone of the original – the Objective C manual calls such an object “functionally independent” from it’s original. Because the mechanisms for making these “intelligent” copies varies from class to class, we ask the objects themselves to perform them. This is the NSCopying protocol.

    You want the latter. If this is one of your own objects, you need simply adopt the protocol NSCopying and implement -(id)copyWithZone:(NSZone *)zone. You’re free to do whatever you want; though the idea is you make a real copy of yourself and return it. You call copyWithZone on all your fields, to make a deep copy. A simple example is

    @interface YourClass : NSObject <NSCopying> 
    {
       SomeOtherObject *obj;
    }
    
    // In the implementation
    -(id)copyWithZone:(NSZone *)zone
    {
      // We'll ignore the zone for now
      YourClass *another = [[YourClass alloc] init];
      another.obj = [obj copyWithZone: zone];
    
      return another;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to copy an object that has a pretty deep hierarchy of member
I need to make a deep copy of an object. The only way I
My need: I want to deep copy all the childs of a single selected
I am just grasping collections and found that the add() methods actually deep copy
I have a std::vector<A*> which I need to deep copy to another vector using
I have been playing around with Object.create in the EcmaScript 5 spec, and I
I need to do a deep copy in Silverlight, which I can do with
Have a class Hero. Sometimes I need a deep copy (when all members are
I want to perform a deep copy on an object, does the clone function
Before C++11 I have used swap-to-back to avoid deep copy overheads, like: vector<vector<Object> >

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.