Pages

Friday, December 18, 2009

Begin with Objective-C

My friend presented me a book: Stephen G. Kochan. "Programming in Objective-C 2.0". So I'm with this book now. After first 15 pages I cannot recommend it or not, but I can say that the Objective-C reality is not scared me anymore, I even like Xcode.
It is unbelievably easy to to make the simplest "Hello, World!" application:
1. In Xcode select New Project in the File menu.
2. In the wizard (or what is the name of this window in Xcode), in the left panel select Command Line Utility.
3. In the right panel select Foundation Tool.
4. Press Choose button in the right-down corner.
5. Type the project name and save the project.
Done.
There is only one file with .m extension - click on it and see the code:
#import <Foundation/Foundation.h>

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

// insert code here...
NSLog(@"Hello, World!");
[pool drain];
return 0;
}
Is it C?
int main, and return in the end are written in C. All other lines? Maybe, the line with NSLog is in C. #import instead of #include, theses lines with the brackets about a pool...

In order to see how this application works, you need to launch the console - it is in the Run menu (or shift+cmd+R). In the console press Build and Go button and see "Hello, World!" text.
The book proposed to modify the text - "Programming is fun!". I did:

I found the application in Finder and launched it:


Let's delete these two lines with the pool,  if we do not understand what is this. Then launch Console from the Run menu and press Build & Go button there - the application still works.

Let's move forward. Next exercise is just from the school - calculate 2 + 2:


Here is the code:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int sum;

sum = 2 + 2;
NSLog(@"2 + 2 = %i", sum);
[pool drain];
return 0;
}

So I'm ready to make school exercises - variable sum is declared as int in the same way we do it in C; NSLog looks similar to TRACE from MFC or just printf.

It's Objective-C, so I have to test an object. It will look so:
#import <Foundation/Foundation.h>

@interface Fraction : NSObject
{
int numerator;
int denominator;
}

-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;

@end

@implementation Fraction

-(void) print
{
NSLog(@"%i/%i", numerator, denominator);
}

-(void) setNumerator: (int) n
{
numerator = n;
}

-(void) setDenominator: (int) d
{
denominator = d;
}

@end


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

Fraction * fraction;
fraction = [Fraction alloc];
fraction = [fraction init];

[fraction setNumerator: 1];
[fraction setDenominator: 3];

[fraction print];

[fraction release];

[pool drain];
return 0;
}

The code above is self-explained. Class Fraction declared between @interface and end. It's derived from NSObject. Two member-variables numerator and denominator are declared as int between the brackets {}. Then three lines with minus sign in the beginning declare three public class methods.

Then, in function main, the variable of type Fraction is declared as the pointer. [Fraction alloc] allocates the memory and [fraction init], obviously, initialize this object. Next lines set nominator and denominator as 1 and 3. So in Objective-C, in order to call a method we need to write [object method]. If the method is called with an argument, it should be [object method : argument].

Line [fraction release] releases the memory.

Now I understand the lines about pool - the first line allocates and initializes the memory and the last line frees the memory.

It's a beginning.

No comments:

Post a Comment