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.

Declaring the interface of a class<br />

101<br />

Although declaring instance variables looks similar to declaring normal variables,<br />

there’s a little more to their syntax than what we’ve discussed. Just as an employee may<br />

not want her boss to know she’s searching for a new job, an object may want to restrict<br />

access to some of its internal data. The declaration of an instance variable can be preceded<br />

by an optional directive such as @private to alter who can access (or alter) its<br />

current value. Table 5.1 outlines the available directives. Once a visibility directive is<br />

specified, it applies to all additional instance variables declared until another accessibility<br />

directive is found. By default, instance variables are @protected.<br />

Table 5.1<br />

Standard visibility directives available for instance variables in <strong>Objective</strong>-C<br />

Visibility level<br />

@private<br />

@protected<br />

@public<br />

@package<br />

Description<br />

Accessible only by the class that declared it<br />

Accessible only by the class that declared it or by any subclass<br />

Accessible from anywhere<br />

Accessible from anywhere in the current code image (the application or static<br />

library in which the class is compiled)<br />

To see why the concept of instance variable visibility is important, imagine that you<br />

had access to a class that represented a loan in a banking system. The bank wouldn’t<br />

be happy if anyone with access to a loan object could reach inside and change the status<br />

instance variable from “rejected” to “accepted” or change the interest rate to –10%<br />

so the bank would pay the loan holder for the privilege of having a loan! Access and<br />

modification of this data would need to be tightly controlled, and this is exactly the<br />

intent of directives such as @protected and @private.<br />

At the other end of the spectrum, you could mark an instance variable as @public,<br />

which means any piece of code can directly access the instance variable with no checks<br />

or balances to ensure it’s done correctly. This practice is usually discouraged because<br />

it provides less control and flexibility over how instance variables are utilized. If direct<br />

access is discouraged, then how are you meant to access or change their values? The<br />

answer leads nicely onto the subject of how to declare methods in a class.<br />

5.2.2 Method declarations<br />

Because allowing direct access to instance variables is discouraged, you must find<br />

another way to allow users to query and alter their values. This alternative technique<br />

should allow you to tightly control access and optionally provide other services such as<br />

logging and validation. One solution is to provide a set of messages that the object can<br />

be sent in order to update or access the instance variables on your behalf. Because the<br />

code implementing the method is part of the class, it’ll have access to the instance<br />

variables no matter their protection level.

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

Saved successfully!

Ooh no, something went wrong!