Changes

Jump to: navigation, search

Copying Objects in Objective-C

65 bytes added, 17:06, 20 November 2009
no edit summary
</pre>
The above code creates a variable named ''account1'' and declares it as being of type ''pointer to object of type BankAccount''. The ''alloc'' and ''init'' method calls create the object in memory and reurn return the address of that object which is then assigned to ''account1''. Clearly, therefore, ''account1'' is not storing the actual object but rather holding a pointer to the memory location of the object.
If we tried to copy the object using the assignment operator, therefore, all we would be doing is copying the address value from one variable to another:
== Copying an Object in Objective-C using the <NSCopying> Protocol ==
In [[An Overview of Objective-C Object Oriented Programming]] the topic of object-oriented programming was covered. In particular the fact that most classes are derived from the ''NSObject'' base class was discussed. The advantage of deriving new classes from ''NSObject'' is that those classes inherit a number of useful methods designed specifically for creating, managing and manipulating objects. Two such methods are the ''copy'' and ''mutableCopy'' methods. These methods use something called the ''<NSCopying> Protocol''. This protocol defines what must be implemented in an object in order for it to be copyable using the ''copy'' and ''mutableCopy'' methods. Classes from the Foundation Framework will typically already be compliant with the <NSCopying> Protocol. We can, therefore, simply call the ''copy'' or ''mutableCopy'' methods to create a copy of an object:
<pre>
On execution of the ''mutableCopy'' method in the above example we will have two independent string objects both of which contain the same string. Because we used the mutable version of the copy method we will be able to modify the contents of ''myString2''. In doing so no change will occur to ''myString1'' because that is an entirely different object.
If we attempted to use either of tehse these copying methods on our own classes without implementing the <NSCopying> protocol the code will fail to run. Take, for example, the BankAccount class created in [[An Overview of Objective-C Object Oriented Programming]]. If we were to create an instance of the class and then try to call the ''copy'' methods we would be presented with a runtime error similar to the following:
<pre>
== <NSCopying> Protocol and copyWithZone Method Implementation ==
The first step in implementing the <NSCopying> protocol is to declare that the class conformsto the protocol. This is achieved in the @interface section of the class. For example:
<tt>@interface BankAccount: NSObject <NSCopying></tt>
== Performing a Deep Copy ==
The copying techniques we have looked at so far in this chapter are referred to as ''shallow copies''. This means that if the ''copy'' or ''mutableCopy'' methods are used to copy an object that itself contains instance variables that are themselves pointers to objects, the copy will also contain pointers to the same objects. To better understand this concept , consider an NSArray object that contains as its elements pointers to three string objects:
<pre>
</pre>
We now have an array name ''myArray1'' that contains as it elements three variables that each point to a string object. We could now create a copy of that array and assign it to variable pointer ''myArray2'':
<pre>
Clearly when we change the object pointed to by element 0 of ''myArray1'' we were also changing the object pointed to by element 0 or ''myArray2''. This proves that even though we created a copy of ''myArray1'' to create ''myArray2'' the pointers contained in the array stayed the same.
In order to create entirely new instance objects we need to perform a ''deep copy''. This can be achieved by using writing the object and its constituent elements to an archive and then reading back into the new object. Our example would, therefore, be rewritten as follows:
<pre>

Navigation menu