Objective-C Tutorial 4 – Classes
As you probably heard on the news this morning, there is a new Objective-C tutorial out. And it’s right here. How cool is that? Last time, we discussed if else statements and basic loops. This time, we’re going to master the art of the class. So what is a class? A class is basically a classification and framework for an object. Objects that are members of a class inherit variables and methods that are declared in the class. To put this into a real life example, you would be an object of the class “Human”. You might inherit variables such as height, gender, age, and weight. Some methods that you might inherit include tying your shoe and walking. Get it yet? Don’t worry if you don’t. That’s what this tutorial is for. Let’s make a class “aliens” (and, in the next tutorial, an object “Bill”).
Interface
The first part of a class is the interface. In the interface, we name the variables and methods of the class. When making a program that adheres to proper Objective-C practices, the interface goes in the header file ( .h ) and is imported into the main ( .m ) file. For the purpose of this tutorial, however, we are going to ignorantly put it in the main ( .m ) file. That’s pretty important to know.
Our class is going to be “Alien”. The variables that we want members of the alien class to have include: number of eyes, age, and number of ears. Methods that members of the alien class are going to inherit will include: setNumberOfEyes, setAge, setNumberOfEars, and output. All of the “set” methods will be used in our program to set the value of the variables. Enough explaining for now, lets look over the code of an interface. Since we are coding only in the main file for now, the interface section will go right under the “#import” line(s).
#import <Foundation/Foundation.h> @interface Alien : NSObject {
int NumberOfEyes;
int Age;
int NumberOfEars;
}
-(void) output;
-(void) setNumberOfEyes: (int) i;
-(void) setAge: (int) a;
-(void) setNumberOfEars: (int) e;
@end
int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; [pool drain]; return 0; } |
@interface Alien : NSObject {
We declare the interface of a class with “@interface” followed by the class name (in this case “Alien”), a colon, and the super class (you really don’t need to know about them yet, don’t worry about it; just use NSObject for your programs until a later date on which I will explain them). The opening curly bracket opens the section of the interface where we will give the variables of the class.
int NumberOfEyes;
int Age;
int NumberOfEars;
}
This is where we declare the variables that members of our class will inherit. If we create an object that is a member of the class “Alien”, then it will have the variables NumberOfEyes, Age, and NumberOfEars. The closing curly bracket closes the part of the interface where we declare the variables.
-(void) output;
-(void) setNumberOfEyes: (int) i;
-(void) setAge: (int) a;
-(void) setNumberOfEars: (int) e;
These are the declarations of the methods for our class. The actual methods will be in the implementation section (which we will go over next). Let’s go over how we declare a method. First, we have a dash followed by what the method will be returning (the “return value”). By returning, I mean that when we call a method in our program, do we want to get any information back from the method to use in our program, or do we just want the code in our method to run. If we don’t want to return anything, we put “void”. If we wanted to return an integer value, we would put “-(int)”. Next, we name out method. In the method “-(void) setNumberOfEyes: (int) i;”, setNumberOfEyes is the name of the method. This will be what we use to call on the method in our program. For methods that don’t need any information (they just run a bit of code when called upon), we would be done now (as is the case of the output method). For other methods, like setNumberOfEyes, we need to declare what type of information we are passing into it as well as the name of the variable that will be equal to the value that we pass into it. In the code of our methods, we can use that variable. If you don’t quite understand what I’m saying, just keep following along and you will get it at some point.
@end
We use @end to end our interface section. Bam. We’re done with the interface. That wasn’t so bad, was it?
Implementation
The second part of a class is the implementation section. In it, we give the methods that we declared in the interface some body. That is, each of the methods that we declared in the interface has to have some code to run when they are called upon, and we write that code in the implementation section. You savvy? Here’s what the implementation section of our program is going to look like:
#import <Foundation/Foundation.h> @interface Alien : NSObject { int NumberOfEyes; int Age; int NumberOfEars; } -(void) output; -(void) setNumberOfEyes: (int) i; -(void) setAge: (int) a; -(void) setNumberOfEars: (int) e; @end @implementation Alien
-(void) output {
NSLog(@"The alien is %i years old and has %i eyes and %i ears.", Age, NumberOfEyes, NumberOfEars);
}
-(void) setNumberOfEyes: (int) i {
NumberOfEyes = i;
}
-(void) setAge: (int) a {
Age = a;
}
-(void) setNumberOfEars: (int) e {
NumberOfEars = e;
}
@end
int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; [pool drain]; return 0; } |
@implementation Alien
We declare the implementation section with a simple @implementation followed by the name of the class that the implementation corresponds to.
-(void) output {
NSLog(@”The alien is %i years old and has %i eyes and %i ears.”, Age, NumberOfEyes, NumberOfEars);
}
-(void) setNumberOfEyes: (int) i {
NumberOfEyes = i;
}
-(void) setAge: (int) a {
Age = a;
}
-(void) setNumberOfEars: (int) e {
NumberOfEars = e;
}
These are the bodies of our methods. In order to tell the computer which lines of code correspond to which methods in the interface, we copy the interface method declaration (minus the semicolon) from the interface and paste it in the implementation section. Then, we surround the code of each method with curly brackets (as shown the code above). As I mentioned in the explanation of the interface, methods that require information to be passed into them have a colon after the method name followed by the data type and the name of the variable that will be equal to the information we are passing into the method. We can then use that variable in the code of each method. For example, the “setNumberOfEyes” method requires that an integer value be passed into it. The variable “i” will be set equal to the value that is passed into the method. In the code of our method, we set the variable “NumberOfEyes” (as declared in our @interface) equal to the variable “i”, thus setting “NumberOfEyes” equal to the integer value that is passed into the method.
You might ask why we are taking the long way around setting the alue of the variables that were declared in the interface section. Well, those variables are “protected” as it were, and can only be accessed by the methods of the same class.
@end
Just like the interface, we end our implementation section with a “@end”.
In our next tutorial, we will go over making objects that are members of our class.
Leave a Comment