Objective C Basic Structure by Example
Objective C is programming language developed by adding object orientation to C programming. A basic hello world example is shown below.
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); } return 0; }
On running the above program, you get an output as shown below.
2014-04-22 22:19:36.546 eezySample[875:303] Hello, World!
Procedure for creating a sample Xcode project for Objective-C
- Open Xcode.
- Select File -> New.
- Select OS X Application tab.
- Select Command Line Tool. Press Next.
- Enter Project Name, Organization Name and Company Identifier and press Next.
- Select location to save and select Create.
Basic structure of Objective C
In Objective C, we use interface and implementation as building blocks for creating classes. Read all the Objective C links here to know more.
/* Use import for including Objective C classes */ #import <Foundation/Foundation.h> // Single line comment in Objective C /* Multi Line comment in Objective C */ // Interface for class with class name eezySampleClass @interface EezySampleClass:NSObject /* NSObject is super class of all Objective C classes*/ { // Instance variables } // Class properties // Instance methods declarations @end // Implementation @implementation EezySampleClass // Instance method definitions @end /* Every Objective C applications have a main and allows command line arguments */ int main(int argc, const char * argv[]) { /* Autoreleasepool used in main for automatic memory management.*/ @autoreleasepool { // A simple log NSLog(@"Hello, World!"); EezySampleClass *eezySampleClass = [[EezySampleClass alloc] init]; NSLog(@"%@",[eezySampleClass class]); } return 0; }
On running the above program, you get an output as shown below.
2014-04-22 22:44:32.407 eezySample[903:303] Hello, World! 2014-04-22 22:44:32.410 eezySample[903:303] EezySampleClass
Don't worry, if you find the above as confusing. You will understand as you go along as others pages are light in content.