Objective-C Tutorial 5 – Objects

6 minute read

Hit the play button on your favorite music video, because it’s time for yet another tutorial on the awesome programming language that is Objective-C! In our last tutorial, we studied classes. In our code, we made the “Alien” class. This time ’round, we’re going to make an object. This object is going to be “Bill”. Bill is going to be a member of the Alien class.

We briefly discussed what objects were in our last tutorial, but let’s go over them again anyways. Objects are basically things that inherit all the characteristics of the class that it is a member of. To put this into a  real life example, “Humans” could be a class of objects. Your best friend Tom would be an object that is a member of the class “Humans”. Tom would inherit (from the Human class) variables like height, gender, age, and weight. Some methods that he might inherit include tying his shoe and walking. Still don’t get it? Let’s go over creating objects that are members of a class.

Making An Object

We are going to continue working with the code from our last tutorial. That code currently consists of the “Alien” class. Just like I promised, we are going to make the Alien “Bill”. Bill is going to be an object that is a member of the Alien class.

When we declared the Alien class, we were working entirely outside of the main function. We are now going to start working in the main function, as that is where objects are declared.

Here is the code we will use to create the object Bill:

#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];
Alien *Bill;
Bill = [Alien alloc];
Bill = [Bill init];
[pool drain];
    return 0;
}

 

Alien *Bill;

We declare an object by giving the name of a class followed by an asterisk and the name of the object you want to create (this is called a pointer, we’ll be going over them in a future tutorial). In this line, we created the object “Bill” that is a member of the class “Alien”.

Bill = [Alien alloc];

In this line, we allocate some memory for the object Bill. That is, we borrow some memory from the computer for our object to use. This is done by giving a declared object’s name, and setting it equal to “[Class alloc]” (replacing “Class” with the name of the class that the object is a member of).

Bill = [Bill init];

Before we can use our object, we have to initialize it. This is done by giving a declared object’s name, and setting it equal to “[Object init]” (replacing “Object” with the name of the object).

That’s all there is to creating an object!

Using Methods

Now that we’ve created our object, we want to use the methods that our object inherited from the Alien class.

#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];
Alien *Bill;
Bill = [Alien alloc];
Bill = [Bill init];
[Bill setNumberOfEyes: 8];
[pool drain];
    return 0;
}

 

[Bill setNumberOfEyes: 8];

To call upon one of the methods that we declared in the Alien class, we put, inside of square brackets, the name of the object followed by the name of the method. If the method needs some kind of value passed into it, then we follow the function name with a colon and the value. In this case, we are using the setNumberOfEyes method and providing it with a value of 8. In our @Implementation section, we made the setNumberOfEyes method take whatever value was passed into it and set the variable NumberOfEyes equal to it. And, just like that, we used the setNumberOFEyes method to give Bill 8 eyes.

Now that we’ve gone over how to use one method, let’s use all of the methods that we declared.

#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];
Alien *Bill;
Bill = [Alien alloc];
Bill = [Bill init];
[Bill setNumberOfEyes: 8];
[Bill setAge: 344];
[Bill setNumberOfEars: 3];
[Bill output];
[Bill release];
[pool drain];
    return 0;
}

 

[Bill output];

The setAge and setNumberOfEars methods are pretty straightforward, but the output method could use a little narration. Unlike the other methods, it doesn’t need any arguments (values passed into it). What it does is take the variables that we set values for (with the other methods) and plugs them into the sentence we declared in the output method in the@implementation section. It then NSLog’s (outputs) the sentence to the console.

[Bill release];

Remember how we allocated, or borrowed, some memory from the computer for our Bill object? Well, now we need to release, or give back, that memory. That is done with the line “[Object release];” (replacing “Object” with the name of the object).

That’s it. We’re done. We created the object Bill. Bill was a member of the Alien class. We ran some methods that set the values of some of Bill’s variables (that he inherited from the Alien class) and then plugged those variables into a sentence and outputted that sentence to the screen. What? You haven’t done that last thing yet? Whoops. Let’s do that now.

If you haven’t opened your console window already, do so now by going to Run -> Console. Next, hit that shiny “Build and Run” button. The sentence  “The alien is 344 years old and has 8 eyes and 3 ears.” should be outputted to the console window.

Feeling brave? try creating your own alien with different numbers of eyes and ears!

YouTube Preview Image

So ya. That’s all folks. See you next time! I’m not yet sure what the next tutorial is going to be about.

Leave a Comment