Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [39]
Object ownership and ARC
To manage these issues, we have the idea of object ownership. When an object has an object instance variable, the object with the pointer is said to own the object that is being pointed to.
From the other end of things, an object knows how many owners it currently has. For instance, in the diagram above, the instance of Person has three owners: the Employee object and the two Child objects. When an object has zero owners, it figures no one needs it around anymore and deallocates itself.
The owner count of each object is handled by Automatic Reference Counting. ARC is a recent development in Objective-C. Before Xcode 4.2, we managed ownership manually and spent a lot of time and effort doing so. (There’s more about manual reference counting and how it worked in the final section of Chapter 20. All the code in this book, however, assumes that you are using ARC.)
Let’s expand the BMITime project to see how ownership works in practice. It is not uncommon for a company to keep track of what assets have been issued to which employee. We are going to create an Asset class, and each Employee will have an array containing his or her assets.
Figure 19.3 Employees and assets
This is often called a “parent-child” relationship: The parent (an instance of Employee) has a collection of children (an NSMutableArray of Asset objects).
Creating the Asset class
Create a new file: an Objective-C subclass of NSObject. Name it Asset. Open Asset.h and declare two instance variables and two properties:
#import @interface Asset : NSObject { NSString *label; unsigned int resaleValue; } @property (strong) NSString *label; @property unsigned int resaleValue; @end Notice the strong modifier on the @property declaration for label. That says, “This is a pointer to an object upon which I claim ownership.” (We’ll talk about other options for this property attribute in Chapter 20.) Remember that when an object doesn’t have any owners, it is deallocated. When an object is being deallocated, it is sent the message dealloc. (Every object inherits the dealloc method from NSObject.) You are going to override dealloc so that you can see when instances of Asset are being deallocated. To make it clear which particular instance of Asset is being deallocated, you’ll also implement another NSObject method, description. This method returns a string that is a useful description of an instance of the class. For Asset, you’re going to have description return a string that includes the instance’s label and resaleValue. Open Asset.m. Synthesize the accessors for your instance variables and then override description and dealloc. #import "Asset.h" @implementation Asset @synthesize label, resaleValue; - (NSString *)description { return [NSString stringWithFormat:@"<%@: $%d >", [self label], [self resaleValue]]; } - (void)dealloc { NSLog(@"deallocating %@", self); } @end Notice the %@ token in the format strings in the code above. This token is replaced with the result of sending the description message to the corresponding variable (which must be a pointer to an object so that it can receive this message). Try building what you have so far to see if you made any errors typing it in. You can build your program without running it by using the keyboard shortcut Command-B. This is useful for testing your code without taking the time to run the program or when you know the program isn’t ready to run yet. Plus, it’s always a good idea to build after making changes so that if you’ve introduced a syntax error, you can find and fix it right away. If you wait, you won’t be as sure what changes are responsible for your “new” bug. Adding a to-many relationship to Employee Now you are going to add a to-many relationship to the Employee class. Recall that a to-many relationship includes a collection object (like