Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
In the last chapter, you created a class called Appliance that had two properties: productName and voltage. Let’s review how those properties work.
In Appliance.h, you declared two instance variables to hold the data:
{
NSString *productName;
int voltage;
}
You also declared accessor methods for them. You could have declared the accessors like this:
- (void)setProductName:(NSString *)s; - (NSString *)productName; - (void)setVoltage:(int)x; - (int)voltage;
However, you used the @property construct instead:
@property (copy) NSString *productName; @property int voltage;
In Appliance.m, you could have implemented the accessor methods explicitly like this:
- (void)setProductName:(NSString *)s
{
productName = [s copy];
}
- (NSString *)productName
{
return productName;
}
- (void)setVoltage:(int)x
{
voltage = x;
}
- (int)voltage
{
return voltage;
}