01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

102 CHAPTER 5 Creating classes<br />

Falling on the side of caution<br />

Although it may be tempting to declare all instance variables as @public, in general,<br />

you should try to keep them as tightly restricted as possible. Once access to an<br />

instance variable is granted, it can be hard to remove or alter the purpose of the ivar<br />

as your application continues to grow and develop. This is especially true if your class<br />

is designed to be part of a widely used framework or support class.<br />

With the default visibility of @protected, a subclass can access any instance variable<br />

declared by its superclass. Once it does, however, an updated version of the<br />

superclass can’t easily remove that variable or alter its purpose. If it does, the subclass<br />

might break, as it now relies on a nonexistent instance variable or at least is<br />

potentially using it in an incorrect manner.<br />

By making the instance variable @private and providing methods to indirectly access<br />

its value, you can isolate the storage of the value from its behavior. If an update to<br />

the superclass means you want to remove the instance variable and instead obtain<br />

its value via a calculation, you can do this in the accessor methods without affecting<br />

any users of the class. As the saying goes, most things in computer science can be<br />

solved by adding another layer of indirection.<br />

For the CTRentalProperty class, you want clients to be able to<br />

■<br />

■<br />

■<br />

Get or set the rental price to an absolute dollar amount<br />

Get or set the address of the property<br />

Get or set the property type<br />

You can easily come up with additional helpful messages that improve the usability of<br />

the class, such as<br />

■<br />

■<br />

Increase the rental price per week by a fixed percentage<br />

Decrease the rental price per week by a fixed percentage<br />

The messages understood by a class are declared in the @implementation section after<br />

the curly braces enclosing instance variables and before the @end directive. This section<br />

is like a list of steps in a recipe. It gives you a general idea of how the final product<br />

is put together, but it doesn’t go into great detail about how each step is achieved.<br />

The simplest form of method declaration is one that expects no parameters and<br />

returns a single value. For example, the following declares a method called rental-<br />

Price, which returns a floating-point number:<br />

- (float)rentalPrice;<br />

The data type of the value returned by the method is enclosed in parentheses and can<br />

be any valid <strong>Objective</strong>-C type, such as those discussed in chapter 2. A special data type,<br />

void, can also be used to indicate that no value is returned.<br />

Now that you have a method declared that allows you to query the current rental<br />

price, it’s only natural to want to complement it with a method to allow you to change

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!