Objective-C: Xcode Programming

Xcode is located in the Developer folder inside a subfolder called Applications.

Following are the steps involved in Writing a Simple Program using Xcode:

1.Start Xcode and select New Project from the File menu.

2.Select the type of application for your project. For creating a console application, select Command Line Tool from the upper-right pane and Foundation from the dropdown list, and click on the choose button as shown in the figure:

3. Create your project by specifying a name say program1 in folder myfolder.

 

 

4. As you click on the save button the project window will open.

5.  Open Program1.m, you will see a default main file as shown in the figure below. Note that .m is the extension of Objective-C source file.
6.Type your program in the edit window and save the changes.

 

7. Now, Build and Run your application by clicking Build and Run,   Build and Go or by clicking Ctrl + Enter from keyboard.
8.If any error message occurs, debug your program and run it again.
9.The console window shows output of the program, if not so than click on the Console option in Run menu. The output of the above program is shown here.

Explanation

The source file of an Objective-C program has an extension .m, where we write the main program as in C. Now, we will illustrate the source code of Program1.m as follows:

The first statement of Program1.m tells the compiler to locate and process a file named Foundation.h.

#import <Foundation/Foundation.h>

#import is similar as to #include in C/C++. It imports or includes the information from the file into the program and is only included once.

int main (int argc, const char *argv[])

main function indicates the beginning point of program execution and int is the return type of the main function. The statement inside the main function is used for command line argument and these parenthesis {} indicates the beginning and end of the main function.

NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]  init];

NSAutoreleasePool is a class used for memory management. It reserves a space in the memory for auto-release pool. Autorelease pools are created just like any other object, using alloc and init.

 // insert code here

It is a comment. Comments are used by developers for future reference and can be given according to their size. They do not have any effect on the program.

For a single line comment, // symbol is used.

For a set of statements, /*                     */ symbol is used.

 NSLog(@“This is my first program.\n This program is made by Vishabh Gupta”);

NSLog – It displays data in the debugger.

NSLog method is similar to that of printf( ) in C-language and @ symbol precedes a string enclosed in “  ” and represents a constant NSString object.

[pool drain];

This statement releases the memory obtained by the object- pool and is automatically inserted in the code as a part of memory management.

return 0;

It terminates the main program and returns a status value 0, means the program executed normally.