ns copying

ref: http://stackoverflow.com/questions/4089238/implementing-nscopying

To implement NSCopying, your object must respond to the -copyWithZone: selector. Here’s how you declare that you conform to it:

Then, in your object’s implementation (your .m file):

What should your code do? First, create a new instance of the object—you can call [[[self class] alloc] init] to get an initialized obejct of the current class, which works well for subclassing. Then, for any instance variables that are a subclass of NSObject that supports copying, you can call [thatObject copyWithZone:zone] for the new object. For primitive types (int, char, BOOL and friends) just set the variables to be equal. So, for your obejct Vendor, it’d look like this:

NSCopying in Base and Sub hierarchy

http://stackoverflow.com/questions/4472904/implementing-nscopying-in-subclass-of-subclass?rq=1
http://stackoverflow.com/questions/18673296/overriding-readonly-property-in-subclass

Base Class

The base is class is straightforward. We only have two member variables, but do not want others to access them. So we declare readonly in @property in the .h file. However, we DO want to modify them in our own implementation. We declare readwrite in our @property in the .m file:

Today.h

Notice the @protected above the member variables. I did this so that children classes can access these member variables.

Today.m

Be sure to synthesize them. Its basically getter/setter = iVar

In our case, since we we did not specify an iVar like this:

dayPerformance_PlacingCount is the iVar
and

makes it run through the setter/getter methods.

So the idea behind copyWithZone is that we need to make a deep copy. Hence everything should have its own address. Staying true to that assertion, we must first dynamically allocate the object to be returned:

We create an empty self class. But wait, What about its member variables? They need to be unique with their own address. So we dynamically create those member variables like so:

Then we have our newly created object’s member variables retain them. the variable copy is our newly created Day variable:

..and then we just return that copy variable.

thus, it gets returned like so:

Thus, yourDay takes on a whole newly allocated Day instance with its own member variables and all.

Base class’s copy method

To use it in your main, you go:

Result:
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 58] – ~~~~~~~~dayOne~~~~~~~~~~~~~
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 59] – dayOne’s address: 0x16dc1f80
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 60] – dayPerformance_PlacingAmount address: 0x16dc1fa0
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 61] – dayPerformance_PlacingAmount value: 1204.500000
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 62] – ~~~~~~~~~~~~~~~~~~~~~

-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 66] – ~~~~~~~~dayTwo~~~~~~~~~~~~~
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 67] – dayTwo’s address: 0x16dc22a0
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 68] – dayPerformance_PlacingAmount’s address: 0x16dc22e0
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 69] – dayPerformance_PlacingAmount’s value: 1204.500000
-[AppDelegate application:didFinishLaunchingWithOptions:] [Line 70] – ~~~~~~~~~~~~~~~~~~~~~

As you can see, the Day addresses are different from the copy. As well as the member variables. In this particular case, I only printed out instance variable dayPerformance_PlacingAmount.

Leave a Reply