Graphics Programs Reference
In-Depth Information
• A variable that points to the object is set to
nil
.
• A variable that points to the object is itself destroyed.
Let's take a look at each of these situations.
Why might a pointer change the object it points to? Imagine a
BNRItem
. The
NSString
that its
itemName
instance variable points to reads “Rusty Spork.” If we
polished the rust off of that spork, it would become a shiny spork, and we'd want to
Figure 3.5 Changing a pointer
When the value of
itemName
changes from the address of the “Rusty Spork” string to
the address of the “Shiny Spork” string, the “Rusty Spork” string loses an owner.
Why would you set a pointer to
nil
? Remember that setting a pointer to
nil
represents
the absence of an object. For example, say you have a
BNRItem
that represents a televi-
sion. Then, someone scratches off the television's serial number. You would then set its
serialNumber
instance variable to
nil
. The
NSString
that
serialNumber
used
to point to loses an owner.
When a pointer variable itself is destroyed, the object that the variable was pointing at
loses an owner. At what point a pointer variable will get destroyed depends on whether it
is a local variable or an instance variable.