Objective-C Tutorial 6 – Returning The Values Of Instance Variables

4 minute read

Hip hip, hooray! It’s time for another Objective-C tutorial. I’m going to try to keep this one short but sweet, as it is intended to add some additional information to our last tutorial. I’ll be talking about using instance variables in our main function.

An instance variable is a variable that is defined within a class. For example, we had the class Alien in our last tutorial. The variable “NumberOfEyes” that was inside of our Alien class is an instance variable.

We can’t access instance variables directly from our main function. Instead, we need to create another method that returns the value of our instance variables (only methods can access instance variables directly). These kinds of methods are referred to as “getter” methods because they “get” the values of the instance variables.

In the program that we’ll be creating, we will replace the “output” method that we used in our last tutorial with  an NSLog statement in our main function. In order to accomplish this, we’ll need to use some getter methods that return the values of our instance variables.

#import <Foundation/Foundation.h>
@interface Alien : NSObject {
int NumberOfEyes;
int Age;
int NumberOfEars; 
}
-(void) setNumberOfEyes: (int) i;
-(void) setAge: (int) a;
-(void) setNumberOfEars: (int) e;
-(int) NumberOfEyes;
-(int) Age;
-(int) NumberOfEars;
@end 
@implementation Alien 
-(void) setNumberOfEyes: (int) i {
NumberOfEyes = i;
} 
-(void) setAge: (int) a {
Age = a;
} 
-(void) setNumberOfEars: (int) e {
NumberOfEars = e;
} 
-(int) NumberOfEyes {
return NumberOfEyes;
}
-(int) Age {
return Age;
}
-(int) NumberOfEars {
return NumberOfEars;
}
@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];

int NumberOfEyes = [Bill NumberOfEyes];
int Age = [Bill Age];
int NumberOfEars = [Bill NumberOfEars];
NSLog(@"The alien is %i years old and has %i eyes and %i ears.", Age, NumberOfEyes, NumberOfEars);
[Bill release]; 
[pool drain];
return 0;
}

 

The above code will output “The alien is 344 years old and has 8 eyes and 3 ears.”

-(int) NumberOfEyes;

-(int) Age;

-(int) NumberOfEars;

As you might recall from our tutorial about classes, we need to declare our methods in the @interface section of the class. All of the methods that we have created prior to this tutorial didn’t need to return anything. Because of this, we put “void” in the parentheses. Now, we want our methods to return the values of our instance variables. All of our instance variables are integers, so we will want to return integers. Similar to how we use “int” to declare an integer variable, we will use “int” to tell the computer that we are going to be returning an integer.

-(int) NumberOfEyes {

return NumberOfEyes;

}

-(int) Age {

return Age;

}

-(int) NumberOfEars {

return NumberOfEars;

}

Now that we’ve declared our methods in the @interface, we need to give our methods some code to run. First, copy the method declaration (minus the semicolon) and paste it in the implementation. Then, open the body of the method with an opening curly bracket. The code that we want inside of our methods is “return instancevariable” (replacing “instancevariable” with the corresponding name of the instance variable that we want the value of). This “return” statement is how we return values in our methods. In our NumberOfEyes method, the value of the instance variable “NumberOfEyes” is returned. When writing code in the main function, we will be able to call this method and, once the “return” statement is run, we will receive the value of “NumberOfEyes”.

int NumberOfEyes = [Bill NumberOfEyes];

int Age = [Bill Age];

int NumberOfEars = [Bill NumberOfEars];

You’ve declared variables before. Nothing new there. But wait, what’s this crazy looking [Bill NumberOfEyes] ? We’re just calling the method “NumberOfEyes”. That’s right, we can set a variable equal to the returned value of a method.

NSLog(@”The alien is %i years old and has %i eyes and %i ears.”, Age, NumberOfEyes, NumberOfEars);

Nothing new here either, we’re just plugging some variables into an NSLog statement.

A Shortcut

Instead of setting variables equal to the returned value of a method, we can just call the methods from inside the NSLog statement.

#import <Foundation/Foundation.h>
@interface Alien : NSObject {
int NumberOfEyes;
int Age;
int NumberOfEars;
}
-(void) setNumberOfEyes: (int) i;
-(void) setAge: (int) a;
-(void) setNumberOfEars: (int) e;
-(int) NumberOfEyes;
-(int) Age;
-(int) NumberOfEars;
@end 
@implementation Alien
-(void) setNumberOfEyes: (int) i {
NumberOfEyes = i;
}
-(void) setAge: (int) a {
Age = a;
} 
-(void) setNumberOfEars: (int) e {
NumberOfEars = e;
} 
-(int) NumberOfEyes {
return NumberOfEyes;
}
-(int) Age {
return Age;
}
-(int) NumberOfEars {
return NumberOfEars;
}
@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];
NSLog(@"The alien is %i years old and has %i eyes and %i ears.", [Bill Age], [Bill NumberOfEyes], [Bill NumberOfEars]);
[Bill release];
[pool drain];
    return 0;
}

 

The above code will output “The alien is 344 years old and has 8 eyes and 3 ears.”

NSLog(@”The alien is %i years old and has %i eyes and %i ears.”, [Bill Age], [Bill NumberOfEyes], [Bill NumberOfEars]);

And there you have it, a much simpler and compact way to get the values of the instance variables and output them to the screen.

Leave a Comment