Programming isn’t something you can learn over night, I know this first hand. I’ve been trying to learn a programming language for quite some time now. Its been on and off through the past 9 months. Objective-C is something I really want to learn because I can develop Mac OSX, iPhone, iPod Touch, and iPad apps. Thats a whole load of options. Im going to tell you right now that I dont know how to program in objective-c yet. But I do know how to create a basic program, as I am reading a book right now. As I learn I will help you guys out as much as I can.
Lets start, shall we? Open up Xcode, if you dont have Xcode then download it from Apples developer site. When Xcodes open select new project then Command line Tool and make sure you have foundation selected from the drop down menu. Name it whatever you would like and save it.
Lets first explain whats going on here. Here you see that you have 5 files, these are all pre-generated from xcode. The only one that we are going to worry about is the file called [filename].m. .m in objective-c means its the footer file, and .h is the header file. There is no need for a header file in this program right now, we will need it when we get into Classes, methods, and objects. But thats later on. So lets focus on the .m file for now.
When you look inside the .m file you should see that there is already code written there. It should read off like this:
#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;
}
Lets explain whats going on here. In the first statement we grab enough memory for NSAutoreleasePool to run. Thats what that nest is doing. Telling the Objective-c compiler that we need to allocate enough memory for this to run. The line that says “//insert code here” is a comment. Comments are not part of the program and are there strictly for reading. The next line is NSLog, NSLog is just a class that Apple has premade. It just reads out the text within the (@”text here”) statement. [pool drain]; is a statement thats telling the compiler to release that memory that we allocated when we said [[NSAutoreleasePool alloc] init];. One note I would like to make. In Objective-C you ALWAYS ALWAYS ALWAYS end a statement with a semi-colon! If you dont the complier wont be able to read the code you just typed and give you an error.
Lets add some code here, up the ante eah?
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];int sum;
sum = 30 + 30;
// insert code here…
NSLog(@”30+30 = %i”, sum);
[pool drain];
return 0;
}
So here you see we introduced some new things. We set “sum” up as an integral value. Sum is going to store the answer of the math problem. First we have to actually say what the problem is. So we say sum = 30 + 30;. So in the NSLog section we actually type out what we want the console to display. So we say NSLog(@”30+30=%i”, sum); The %i is what the complier uses to see that its an integer. If we didnt use this then it would give an error. The next thing we need to declare is that we are actually using “sum” where we stored the answer. You do this by saying “,sum);“ after the NSLog statement. There you go! Thats a simple program that you just built using Objective-C! Have any questions? Please leave a comment or email me asking it, more than willing to help!
Popularity: 26% [?]



