Learning Objective-C Part 2 – Our First Program

Welcome to part two of my tutorials for learning Objective-C. Before proceeding, I highly recommend making sure you’ve read Learning Objective-C, Part 1 – Introduction To Objective-C as it covers a lot of vital information about the language itself that will be beneficial for you to know before continuing on with lesson 2. Anyhow, in this lesson, we’re going to begin working with our first program.

Screen shot 2013-04-30 at 3.27.15 PM

To start, you’re going to want to launch a program in Xcode, your settings should be similar to above. We are going to be working with a command line tool Mac application that runs in a console. Make sure the type is set to ‘Foundation’ as that tells Xcode that you are programming in Objective-C and not another language such as C or C++ Of course, you are free to name it whatever you want.

Screen shot 2013-04-30 at 3.27.47 PM

 

When you first launch your project, it will look similar to this.  Let’s take a look at some of 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;
}

Compiling and running this program as it is now will simply open a console message that says, “Hello World”. For demonstration purposes, let’s change it up a little bit.

Our First Program

 

 #import <Foundation/Foundation.h>
 
 int main (int argc, const char * argv []) {
 NSAutoReleasePool * pool = [[NSAutoReleasePool alloc] init];
 
 //insert code here...
 
 //this is a comment
 NSLog(@"iOSUniverse is the best website ever!");
 [pool drain];
 
 /* This is 
    also 
a 
             comment
   */
 return 0;
}

As you can see, we’ve changed quite a bit. Compiling and running the program now will display something like this:

Screen shot 2013-04-30 at 3.30.20 PM

 

As we’ve changed it up, we’ve added some lines of codes called comments. Comments are used to help organize your code, or help you remember what’s going on, which is especially helpful if you have thousands of lines of codes. The compiler will skip over comments as if they weren’t there. Single line comments are signaled by using the two characters //, and multiple line comments are signaled by text wrapped in /* and */.

I’ve also separated out the code a few lines. As you can see, it doesn’t matter if there’s spaces in between the lines of code. You could have no space at all, or you can have 20 empty lines, it doesn’t matter. Spacing out your codes can make it easier for you to read. It also doesn’t matter how far indented the code is – whatever is easiest for you as the programmer to read is what’s best.

We’ve also changed the text in the NSLog statement. NSLog will take the text wrapped in the quotes and display it as an output. (Similar to printf in C, and cout in C++). You can change this text to say whatever you want.

The other lines of code within our first program aren’t that important to you right now. They have to do with memory management and command line arguments, something we will get into in a later tutorial.

And there you have it, Lesson 2 complete. Although we have yet to make a program that does much, you have learned some core programming fundamentals that will make programming more complex programs in the future a heck of a lot easier. If you have any questions, please visit the Programming section of the iOS Universe Forums

.

Want to learn how to make thousands of dollars of passive income in the App Store?

My friend Mike over at App Dev Secrets just released his new, fool-proof course that teaches you how to make amazing iPhone or iPad applications and games! Best of all, you need zero programming knowledge whatoever to get started!

If you've ever dreamed of having your own apps and games make thousands every month, head on over to Mike's website and check it out!

Speak Your Mind

*