Tutorial 2-2: Properties and Accessor Methods
In this tutorial, we will need to use the project that we started in the last tutorial. So go ahead and open your “Objects Intro” project.
If you remember, last time we created the Television class which we used to create a Television object which could print out two different statements. Today we will add a new method that will change the channel of the television. To do this the television needs to be able to store some data. Let’s go ahead and create a space to store that data now. Open “Television.h” and edit the empty brackets to look like this:
@interface Television : NSObject { int channel; }
We have now got an instance variable in which to store the channel number. An instance variable is data held by an object. For example, if we created two Television objects from the Television class, we could set the channel number of myTV1 to 3, and the channel number of myTV2 to 7. Both Televisions are produced by the Television class, but each can store their own data. But how do we change the channel? This is where Accessor Methods come in.