Using X-Code.
“Create a New Xcode Project” from startup screen (or File->New->New Project).
Command Line Tool application type.

Product name: prog1
Company identifier: Whatever!

Ensure that Foundation is select as the Type.
Ensure Use Automatic Reference Counting is selected.

Choose your location for your project.

Left hand pane, main.m. Open this and use this as the home for the first app.

  • Extension; Meaning
  • .c; C language source
  • .cc, .cpp; C++ language source
  • .h; Header file
  • .m; Objective-C source
  • .mm; Objective-C++ source
  • .pl; Perl source
  • .o; Object (compiled) file

The first application:

//
//  main.m
//  prog1
//
//  Created by Duncan Bowring on 1/15/12.
//

#import <Foundation/Foundation.h>

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

@autoreleasepool {

// insert code here...
NSLog(@"The first app");

}
return 0;
}

Under the View toolbar menu, middle icon is to show the window which will display the program output. Mouse tooltip should say “Hide or show the Debug area”.
Xcode should display this window whenever anything is written to the debug area. Here is the output from the first app:

2012-01-21 19:45:52.626 prog1[1355:707] The first app

 

Program Explanation

  • Objective-C the code is case sensitive.
  • White space doesn’t matter; Objective-C does not care where on the line the code is entered.
  • The first 6 lines of the program are comments. Comments are not executed by the compiler and are used to document the program for the person reading the source code.
    • Two ways of entering comments:
      • // (two slashes) which are for single line comments.
      • /* to begin a comment block; this marks the beginning of the block.
      • */ will mark the end of the comment block and terminate the comment.
        • Handy if you want multi line comments. /* Comments cannot be nested.
  • Next line of code tells compiler to find and process/include a file named Foundation.h.
    • This is a system file.
    • Foundation.h contains information about other classes/functions that are used later in the program.
  • main is a special name indicating where the program is to begin execution. int specifies the type of value main returns. It is an integer.
  • The contents of ( ) are for command line arguments which is part of the C language.
  • The main routine’s program statements are contained within the braces { }
  • @autoreleasepool
    • Anything executed within the { } is within a context called autorelease pool. The autorelease pool is a mechanism which allows system to manage the memory the application uses as it creates new objects.
  • The statement NSLog is called within the routine and has a string argument; @”The first app”. The @ sign and the “” makes this known as a constant NSString object. Without the @ you will be writing a constant C-style string. With it you are writing an NSString string object.
  • NSLog routine is an Objective-C function that logs the arguments. Before it logs them it displays date and time the routine is executed, the program name and other data relating to process.
  • All statements in Objective-C must be terminated with a semi-colon.
  • Final code statement is return 0;. – This tells main upon termination to return a status value of 0. 0 as a return code means the program ended without error.

Summary of the whole process:

  1. Start XCode.
  2. Create new project; File->New->New Project or Create New Xcode Project from startup screen.
  3. Type of application, select Application->Command Line Tool.
  4. Enter name of application and set type to Foundation. Ensure Use Automatic Reference Counting is checked.
  5. Select name for project folder and choose directory to store project files in.
  6. Left hand window, main.m. It may be hidden in the tree view. Click the file and type program into the edit window that appears in the rightmost pane.
  7. In toolbar, select middle icon under View. This will show Debug window. This is where program output will show.
  8. Build and run the application by clicking Run button in toolbar.
  9. If any compiler errors show here, change the program code and run it again.

Displaying Variable Values

NSLog can also display the variable values and results of computations.

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

{

@autoreleasepool {

int sum;

sum = 10 + 15;

NSLog(@”The sum of 10 and 15 is %i”, sum);

}

return 0;

}

Program Output:

2012-01-21 20:25:32.175 prog1[2193:707] The sum of 10 and 15 is 25

Program ended with exit code: 0

int sum; declares the variable sum to be set up of type integer. All variables must be declared before they can be used.
Variable declaration tells the compiler how the program should use it. Type int can only hold integer values – basically a number without a remainder/floating point only.
The number 15 is added to the number 10 and the computational result is stored (as indicated by the = sign) in the variable called sum.
NSLog routine has two arguments enclosed within the ( ) brackets. These arguments are separated by the comma.
The first argument of NSLog is the string. We want to have the value of variable sum displayed after the constant string characters defined here. The % character is a special character that NSLog recognizes. The character immediately following the % sign specifies what type of value to display at this point in the string. The letter i signals that an integer is to be displayed. When NSLog finds %i within the first parameter (the string), it will display the value of the next argument to the routine. sum is the next argument to NSLog, the value is automatically displayed after the initial string of “The sum of 10 and 15 is “.

You can stack these. For example, you can have multiple %i %i %i %i within the first parameter (character string), and afterwards have , variable1, variable2, variable3, variable4 and it will display as such.

Some other Programming Exercises:

1. Write a program that displays the following text:

In Objective-C, lowercase letters are significant.
main is where program execution begins.
Open and closed braces enclose program statements in a routine.
All program statements must be terminated by a semicolon.

Source Code:

#import <Foundation/Foundation.h>

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

{

@autoreleasepool {

NSLog(@”In Objective-C, lowercase letters are significant.\nmain is where program execution begins.\nOpen and closed braces enclose program statements in a routine.\nAll program statements must be terminated by a semicolon.”);

}

return 0;

}

Code Output:

In Objective-C, lowercase letters are significant.

main is where program execution begins.

Open and closed braces enclose program statements in a routine.

All program statements must be terminated by a semicolon.

2. What output would you expect from the following program?

Source Code:

#import <Foundation/Foundation.h>

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

{

@autoreleasepool {

int i;

i = 1;

NSLog (@”Testing…”);

NSLog (@”….%i”, i);

NSLog (@”…%i”, i + 1);

NSLog (@”..%i”, i + 2);

}

return 0;

}

Code Output:

2012-01-21 21:45:24.596 prog1[2997:707] Testing…

2012-01-21 21:45:24.597 prog1[2997:707] ….1

2012-01-21 21:45:24.599 prog1[2997:707] …2

2012-01-21 21:45:24.600 prog1[2997:707] ..3

3. Write a program that subtracts the value 15 from 87 and displays the result, together with an appropriate message.

Source Code:

#import <Foundation/Foundation.h>

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

{

@autoreleasepool {

int sum;

sum = 8715;

NSLog (@”87 – 15 = %i”,sum);

}

return 0;

}

Code Output:

2012-01-21 21:49:14.405 prog1[3093:707] 87 – 15 = 72

 

4. Identify the syntactic errors in the following program. Then type in and run the corrected program to make sure you have identified all the mistakes:

Source Code:

#import <Foundation/Foundation.h>

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

{

@autoreleasepool {

INT sum;

/* COMPUTE RESULT //

sum = 25 + 37 – 19

/ DISPLAY RESULTS /

NSLog (@’The answer is %i’ sum);

}

return 0;

}

Answer:

  • Upper case INT; should be lower case.
  • No close to quote block; // instead of */
  • No semicolon ending statement after computation; sum
  • Invalid single line quote; / instead of //
  • No double quote for character string as first parameter of NSLog.

Correct Source Code:

#import <Foundation/Foundation.h>

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

{

@autoreleasepool {

int sum;

/* COMPUTE RESULT */

sum = 25 + 3719;

// DISPLAY RESULTS /

NSLog (@”The answer is %i”, sum);

}

return 0;

}

Code Output:

2012-01-21 21:55:31.080 prog1[3238:707] The answer is 43

 

Onto 2.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s