Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

V. Advanced Objective-C > 30. Properties

30
Properties

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;
}

  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial