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.

Providing an implementation for a class<br />

107<br />

them by their name. The following is a possible implementation of CTRentalProperty’s<br />

setRentalPrice: method:<br />

- (void)setRentalPrice:(float)newRentalPrice {<br />

rentalPrice = newRentalPrice;<br />

}<br />

Notice how the assignment statement can assign a new value to the rentalPrice<br />

instance variable without any fuss. You may wonder how this statement knows which<br />

object to update in the case that multiple CTRentalProperty objects have been created.<br />

You don’t pass in any reference to an object of interest. The answer is that,<br />

behind the scenes, every instance method is passed two additional hidden parameters<br />

called self and _cmd. We leave discussion of _cmd for chapter 8, but self is the magic<br />

that enables the method to know which object it should work with. You could explicitly<br />

make this linkage more apparent by rewriting the setRentalPrice: method as follows:<br />

- (void)setRentalPrice:(float)newRentalPrice {<br />

self->rentalPrice = newRentalPrice;<br />

}<br />

The -> operator allows you to access the instance variables associated with the variable<br />

referenced on the left-hand side. Because the compiler can pass different objects into<br />

the method via the hidden self parameter, it can change which object the method<br />

works with.<br />

Check out your debugger window<br />

The next time you use the Xcode debugger and hit a break point, check out the Arguments<br />

section in the variables panel. You should be able to clearly see the self and<br />

_cmd parameters listed.<br />

Congratulations! You’re well on your way to becoming an <strong>Objective</strong>-C guru. Another<br />

slightly mysterious piece of the IDE suddenly makes a little more sense.<br />

Knowing that self is a hidden parameter that always represents the current object is<br />

handy, but as demonstrated previously, it isn’t typically necessary to use it to access<br />

instance variables. There is, however, another practical use of self that’s unavoidable.<br />

5.3.3 Sending messages to self<br />

When implementing an instance method, you may wish for it to call on the services of<br />

other methods defined by the class. But to send a message, you must first have a reference<br />

to the object you want to send it to. This reference is usually provided in the<br />

form of a variable. When you want to refer to the current object, whatever it may be,<br />

the hidden self parameter neatly fills the role, as follows:<br />

- (void)handleComplaint {<br />

NSLog(@"Send out formal complaint letter");<br />

numberOfComplaints = numberOfComplaints + 1;

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

Saved successfully!

Ooh no, something went wrong!