Classes, Methods, and instances in Objective-C. (Part 1)

In our last tutorial on Objective-c we showed you just the bare basics on classes, now lets dive in a little deeper. I’m going to try and teach this as basic as I can, though this tutorial will be a little lengthy thats why this is broken up into three different sections. We are going to deal with the @interface section today. You should read the first tutorial here before moving on. There are key concepts in that tutorial that you really need to learn before trying to move on to more advanced classes, methods and instances. Lets start here. Open up a new project and do the same as the last tutorial, that is, go to the command line template and make sure you have foundation selected.

Now that we have our project open and saved lets get to work. We are going to add to the .m file like we did last time, but only now we are going to add three sections. All objective-c programs are broken into three sections, @interface, @implementation, and the program section. Like I said we are only going to mess with the @interface section today. Every time you see the @interface section you will always see it in this format:

@interface Class : SuperClass
{
instance variables here;
}
methods declared here;
@end

In our case we are going to make a simple program that deals with fractions. The class name will be Fraction and the superclass will be NSObject. Then we are going to make two instance variables one called “num” and the other called “dem”. These are short names for numerator and denominator, if you would like you can type them out fully but I prefer to shorten their names. Like we learned in the last tutorial sense these are going to be whole numbers they are going to be called by integers. So it should like this so far:

@interface Fraction : NSObject
{
int num;
int dem;
}

Now we have to declare our methods that we are going to “define” in the @implementation section. All this means is that we are telling the complier that we are going to use these later and actually tell it what we are going to do with these methods. Usually we call these by saying:
-(void) print;
All that is saying is this isn’t going to return a value. If it did return a value we wouldn’t use “void”. The last part “print” is what we are calling our method to display the fraction in NSLog. More on this later in the series. So now that we have our print method declared we can make our methods that we will use to set our numerator and denominator. Using the same -(void) call we did for the print we are going to say:
-(void) setNum: (int) n;
This is a method argument. Our (void) print; method wasn’t an argument because we ended it after print with a semi-colon, notice the difference in symbols. For a method argument you have to use a colon and to just have a method end it with the semi-colon. That method argument we declared we will use later on to set the numerator for our fraction. As you can guess we are going to use the same format to set our denominator. The last part of our method argument where it says “(int) n;” we use int because its storing a number. So our whole section should look like:

@interface Fraction: NSObject
{
int num;
int dem;
}
-(void) print;
-(void) setNum: (int) n;
-(void) setDem: (int) d;
@end

Notice we always have to end our sections with @end. This tells the complier that the section has, well, ended. If any of your code comes after that you will get a error and it can make your debugging hell when late at night.

There you go we are done with the first part of this three part tutorial. In our next part we will learn more about the @implementation section.

[ad]

Popularity: 27% [?]

You can leave a response, or trackback from your own site.
blog comments powered by Disqus