This is for those who are already familiar with object oriented programming languages such as C++, Java or C# who wish to learn Objective-C from what is already known. I'll be discussing the similarities and differences in Objective-C with respective to those programming languages so that within minutes, you can learn the necessary concepts easily.
So let's begin with a Hello World program in Console.
#import<Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog (@"Hello World");
[pool drain];
return 0;
}
Now lets discuss a little bit about the above program. At the top of the program we have to import the header files we need. In other programming languages we do it as follows.
Language | Command |
---|---|
C | #include<stdio.h> |
C++ | #include<iostream> |
C# | using System.io; |
Java | import java.lang.*; |
In Objective-C, we put #import<Foundation/Foundation.h> .
Next thing is the main method. It is same as the main method definition in C or C++.
At the moment you don't need to worry about what the NSAutoreleasePool is. I'll discuss about that in a later post. Just keep in mind that it is related to allocation of memory for the program. [pool drain] is de-allocating memory.
Here's the important thing, printing something on screen.
Language | Command |
---|---|
C | printf("Hello World"); |
C++ | cout<<"Hello World"; |
C# | Console.WriteLine("Hello World"); |
Java | System.out.println("Hello World"); |
In Objective-C, we use NSLog( ) method to print something on the screen. NS stands for "Next Step". In other programming languages, we use double quotes to wrap the string of text. In Objective-C, we do the same and additionally we put an @ sign in-front of the string.
NSLog (@"Hello World");
Since the main method's return type is int, return 0; is used at the end of the program.
From my next post, I'll discuss about the data types in Objective-C.