Graphics Programs Reference
In-Depth Information
Build and run the application. You should see the exact same behavior as the last time you
ran it. The only difference is that BNRItem.h is much cleaner.
Synthesizing properties
In addition to using a property to declare accessor methods, you can synthesize a property
to generate the code for the accessor methods in the implementation file. Right now,
BNRItem.m defines the accessor methods declared by each property. For example, the
property itemName declares two accessor methods, itemName and setItemName: ,
and these are defined in BNRItem.m like so:
- (void)setItemName:(NSString *)str
{
itemName = str;
}
- (NSString *)itemName
{
return itemName;
}
When you synthesize a property, you don't have to type out the accessor definitions. You
can synthesize a property by using the @synthesize directive in the implementation
file. In BNRItem.m , add a synthesize statement for itemName and delete the imple-
mentations of setItemName: and itemName .
@implementation BNRItem
@synthesize itemName;
- (void)setItemName:(NSString *)str
{
itemName = str;
}
- (NSString *)itemName
{
return itemName;
}
You can synthesize properties in the same synthesize statement or split them up into mul-
tiple statements. In BNRItem.m , synthesize the rest of the instance variables and delete
the rest of the accessor implementations.
@implementation
@synthesize itemName;
@synthesize containedItem, container, serialNumber, valueInDollars,
dateCreated;
- (void)setSerialNumber:(NSString *)str
{
Search WWH ::




Custom Search