Entering The World Of Objective-C
Welcome to Colour Me Cocoa.
Looks like you're new here. If you like what you see, you may want to subscribe to my RSS feed, or if you prefer sign up to receive email updates.
Thanks for visiting!
Up until now we have been writing code exclusively in the C language. C has served us well, but it is now time to move on. Objective-C is the language of choice for Cocoa application developers.
What Is Objective-C?
Objective-C is an extension of the C language. Everything that we have learned so far, works equally well in Objective-C. What Objective-C offers us is a new way of thinking about code. It allows us to program at a higher level using Object Oriented Programming Techniques. The Apple Developer Connection has a great introduction to the Objective-C Language which I recommend reading.
Creating A Program Using Objective-C
When we built our first program we created a Standard Tool from the Command Line Utility section of the New Project Wizard. This gave us the following Hello World! program:
#include <stdio.h>
int main (int argc, const char * argv[]) {
// insert code here...
printf("Hello, World!\n");
return 0;
}
Today we are going to repeat that first program, only this time instead of choosing the Standard Tool, select Foundation Tool. I’m calling mine MyFirstFoundation.
The project files you are presented with differ slightly from the Standard Tool, but the one we are interested in is still the .m file.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSLog(@"Hello, World!");
[pool release];
return 0;
}
That code is similar, yet different. Don’t be afraid though, it’s not hard at all.
Familiarizing Yourself With Objective-C
When run, both of these programs will produce the same result. The string Hello, World! will be printed to the screen and the program will terminate. The differences lay in the use of Objective-C.
Line 1 has changed. Instead of including the stdio.h library we are instead importing the Foundation/Foundation.h library. Slightly different code, effectively the same though. We’re telling our program that we are going to use part of the Foundation library.
Line 3 is identical, which means less learning. Hooray! Line 4 on the other hand is completely new. It uses the first of two calls to a Foundation Library class. They all start with NS, which stands for NextStep, the original object-oriented, multitasking operating system.
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
So what does it do? It creates, allocates memory (alloc) and initializes (init) an NSAutoreleasePool named pool. Confused yet? Probably.
The good news is that this program doesn’t even use pool, it just sits there doing nothing. This means that if you want to ignore it, and pretend it doesn’t exist, you can.
If you don’t like ignoring things then here’s a brief explanation. Programs use memory on your computer, every time you create a new object, you need some memory to store it. The NSAutoreleasePool is a mechanism for releasing the memory your program uses when it is finished with it. This means that when your program terminates, it isn’t still hogging systems resources.
I’d love to go into more detail right now, but since we are not going to use the pool there really isn’t much point. This ADC article should be more than enough to fill the void if you’re after more information right now.
// insert code here... is a comment we’ve seen before. Line 7 replaces our printf statement with a call from the Foundation Library
NSLog(@"Hello, World!");
Like printf, NSLog is used to print strings to the screen. One difference is that NSLog only prints to the screen during debug mode. Another difference you will notice is the use of the @ symbol before Hello, World! string. This is how strings are identified in Objective-C.
[pool release];
Line 8 releases our NSAutoreleasePool object, freeing up any memory that was in use (none for this program). The rest of the program is the same as our original C program.
Homework?
Want something to do by yourself? You got it! Since Objective-C is an extension of C, everything we’ve already covered applies to the MyFirstFoundation project as well.
This means you can work through the previous C programming articles and apply the same enhancements to MyFirstFoundation in your own time. Loops, flow control, functions, multiple files… get cracking!
As always, if you’ve got any questions then leave a comment and I’ll try my best to help you out. Let me know what you thought about this article by using the new ratings plugin, or leave a comment.