Wednesday, May 8, 2013

How To Create A Class In Objective C (Xcode)

How To Create A Class In Objective C (Xcode)



Note 1: Every bit of code that is red means that it's the new code that's been added since last time.

Note 2: Everything mentioned here will only work in a Command Line Tool - Foundation project type.


So, let's begin by importing the Foundation file. This is just a bunch of code required for any Objective C program to run, so instead of writing it down each time we can just load the file that contains it:

#import <Foundation/Foundation.h>

Then, it's time to make the interface!

At first, let's name the class and write the variables:

#import <Foundation/Foundation.h>

@interface Person: NSObject{ int age; int weight; }

As you can see, we're making a class called "Person", and the only variables that we need are the person's age and weight.


Now let's go ahead and make the methods (aka what a "Person" can do, like "brush your teeth" or "eat" etc. - we'll keep it real simple though):

#import <Foundation/Foundation.h>

@interface Person: NSObject{
 int age;
 int weight;
}
-(void) print; -(void) setAge: (int) a; -(void) setWeight: (int) w; @end

Since the interface is done, we can go ahead and make the implementation!

Firstly, we need to write the "print" method, which just takes the values of the "age" and "weight" variables and prints them on the screen:

#import <Foundation/Foundation.h>

@interface Person: NSObject{
 int age;
 int weight;
}
-(void) print;
-(void) setAge: (int) a;
-(void) setWeight: (int) w;
@end

@implementation Person -(void) print{ NSLog(@"I am %i and weigh %i pounds", age, weight); }

Next, we need to do the same for the setAge and setWeight methods:

#import <Foundation/Foundation.h>

@interface Person: NSObject{
 int age;
 int weight;
}
-(void) print;
-(void) setAge: (int) a;
-(void) setWeight: (int) w;
@end

@implementation Person
-(void) print{
 NSLog(@"I am %i and weigh %i pounds", age, weight);
}
-(void) setAge: (int) a{ age=a; } -(void) setWeight: (int) w{ weight=w; } @end

What they do, is that they take what's into the temporary variables ("a" and "w") and store it into the main variables ("age", "weight").


Now, we finally start making the actual objects!

First, let's make the main method:

#import <Foundation/Foundation.h>

// interface
@interface Person: NSObject{
 int age;
 int weight;
}
-(void) print;
-(void) setAge: (int) a;
-(void) setWeight: (int) w;
@end

// implementation
@implementation Person
-(void) print{
 NSLog(@"I am %i and weigh %i pounds", age, weight);
}
-(void) setAge: (int) a{
 age=a;
}
-(void) setWeight: (int) w{
 weight=w;
}
@end

int main(int argc, char *agrV[]){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init]; }

Now, the object code:

#import <Foundation/Foundation.h>

// interface
@interface Person: NSObject{
 int age;
 int weight;
}
-(void) print;
-(void) setAge: (int) a;
-(void) setWeight: (int) w;
@end

// implementation
@implementation Person
-(void) print{
 NSLog(@"I am %i and weigh %i pounds", age, weight);
}
-(void) setAge: (int) a{
 age=a;
}
-(void) setWeight: (int) w{
 weight=w;
}
@end

// main program
int main(int argc, char *agrV[]){
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
 
Person *nicolas; nicolas = [Person alloc]; nicolas = [nicolas init]; [nicolas setAge: 25]; [nicolas setWeight: 350]; [nicolas print]; [nicolas release]; [pool drain]; return 0;
}

So what we did here is: First, we declared the object. Then, we allocated (aka borrowed) some memory from the computer,and then we initialized it. Next, we used the "setAge" and the "setWeight" methods to get values for our original variables, and then we used the "print" method to, well, print the values, and then we released any memory that was held by our object. Last but not least, we drained our pool of memory and returned 0, which means that our program ended perfectly.

Now let's go ahead and add another object, but using a slightly different technique this time (which I highly suggest you should use):

#import <Foundation/Foundation.h>

// interface
@interface Person: NSObject{
 int age;
 int weight;
}
-(void) print;
-(void) setAge: (int) a;
-(void) setWeight: (int) w;
@end

// implementation
@implementation Person
-(void) print{
 NSLog(@"I am %i and weigh %i pounds", age, weight);
}
-(void) setAge: (int) a{
 age=a;
}
-(void) setWeight: (int) w{
 weight=w;
}
@end

// main program
int main(int argc, char *agrV[]){
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
 Person *nicolas;
 
Person *james = [[Person alloc]init];
nicolas = [Person alloc]; nicolas = [nicolas init]; [nicolas setAge: 25]; [nicolas setWeight: 350]; [nicolas print]; [nicolas release];
[james setAge: 57]; [james setWeight: 310]; [james print]; [james release];
[pool drain]; return 0; }

So, now we want to add two more "getter methods". They are simply going to return the "age" and "weight" variables. This, though, allows us to access these vars, which makes it easier to print stuff out. Let's see:

#import <Foundation/Foundation.h>

// interface
@interface Person: NSObject{
 int age;
 int weight;
}
-(void) print;
-(void) setAge: (int) a;
-(void) setWeight: (int) w;
-(int) age;
-(int) weight;
@end

// implementation
@implementation Person
-(void) print{
 NSLog(@"I am %i and weigh %i pounds", age, weight);
}
-(void) setAge: (int) a{
 age=a;
}
-(void) setWeight: (int) w{
 weight=w;
}
-(int) age{
 return age;
}
-(int) weight{
 return weight;
}
@end

// main program
int main(int argc, char *agrV[]){
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
 
Person *james = [[Person alloc]init]; [james setAge: 57]; [james setWeight: 310]; NSLog(@"James is %i years old and weighs %i pounds", [james age], [james weight]);
[pool drain]; return 0; }

And that's how to create a Class!

No comments:

Post a Comment