O'Reilly logo

Programming in Objective-C, Fifth Edition by Stephen G. Kochan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

18. Copying Objects

This chapter discusses some of the subtleties involved in copying objects. We introduce the concept of shallow versus deep copying and discuss how to make copies under the Foundation framework.

Chapter 8, “Inheritance,” discussed what happens when you assign one object to another with a simple assignment statement, such as here:

origin = pt;

In this example, origin and pt are both XYPoint objects with two integer instance variables called x and y.

Recall that the effect of the assignment is to simply copy the address of the object pt into origin. At the end of the assignment operation, both variables point to the same location in memory. Making changes to the instance variables with a message such as

[origin setX: 100 andY: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required