01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Declaring the interface of a class<br />

103<br />

By sticking with conventions, you won’t rock the boat<br />

In <strong>Objective</strong>-C it’s traditional to name methods that return the value of instance variables<br />

the same as the instance variables themselves.. This is unlike languages such as Java<br />

in which such methods would commonly use a “get” prefix, as in getRentalPrice.<br />

You can name your methods using any convention you like, but by using the time-honored<br />

<strong>Objective</strong>-C conventions, you’ll have a smoother voyage as you develop your<br />

application. Many features of Cocoa Touch, such as Core Data, Key-Value Coding,<br />

and Key-Value Observing, rely in part on language conventions—or at least work nicer<br />

out of the box if these conventions are respected. If you use other conventions, you<br />

may discover that you have additional work to do before being able to fully use all<br />

features of <strong>Objective</strong>-C or Cocoa Touch.<br />

the rental price. To do so, you need to specify that the method expects a parameter,<br />

or argument:<br />

- (void)setRentalPrice:(float)newPrice;<br />

In a method declaration, a parameter is introduced by the colon (:) character, which<br />

is followed by the parameter’s data type and name. In this case you’ve declared a<br />

method called setRentalPrice:, which has a single parameter called newPrice of<br />

type float.<br />

It’s possible to declare a method that expects multiple parameters. For example,<br />

you could declare a method to decrease the rental price by a fixed percentage, ensuring<br />

it doesn’t drop below a predetermined minimum value, by declaring a method<br />

as follows:<br />

- (void)decreaseRentalByPercent:(float)percentage withMinimum:(float)min;<br />

This code declares a method named decreaseRentalByPercent:withMinimum: that<br />

accepts two parameters (percentage and min). Notice how the position of each colon<br />

character in the name indicates where a parameter is expected and that the method<br />

name is interspersed between the parameter declarations rather than being a single<br />

identifier at the start of the declaration.<br />

It’s important to realize that in a method signature, the colons aren’t optional. As<br />

an example, the identifiers rentalPrice and rentalPrice: identify different methods.<br />

The first accepts no parameters, while the second expects a single parameter (as<br />

indicated by the colon). It’s possible (although rather confusing) for a class to declare<br />

both methods.<br />

It isn’t essential to provide a part of the method name before each parameter. For<br />

example, the following declaration is equally valid even though no part of the method<br />

name is provided before the last argument named min:<br />

- (void)decreaseRentalByPercent:(float)percentage :(float)min;<br />

This alternative method is named decreaseRentalByPercent:: and is generally considered<br />

bad form because the name of the method is less self-documenting than

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

Saved successfully!

Ooh no, something went wrong!