Monday, August 31, 2009

looping and decision making

the for loop

#import <Foundation/Foundation.h>

int main(int char agrc,char *argv)

{

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

int number,rightmost;                                                          NSLog(@”enter your number”);                                        scanf(“%i”,&number);                                                         for(;number!=0;number/=10)                                   {rightmost=number%10;}                                                        NSLog(@”%i”,rightmost); 

}
[pool drain];
return 0;

 

}

 

the while loop

// Program to reverse the digits of a number
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, right_digit;
NSLog (@”Enter your number.”);
scanf (“%i”, &number);
while ( number != 0 ) {
right_digit = number % 10;
NSLog (@”%i”, right_digit);
number /= 10;
}
[pool drain];
return 0;
}

the do while loop

// Program to reverse the digits of a number
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, right_digit;
NSLog (@”Enter your number.”);
scanf (“%i”, &number);
do {
right_digit = number % 10;
NSLog (@”%i”, right_digit);
number /= 10;
}
while ( number != 0 );
[pool drain];
return 0;
}

//output

Enter your number.
1456
6
5
4
1

the break and continue

i am not going to explain these thing same as “C”

u can find these else where as well million of site to help u in loop

if loop 

// Program to determine if a number is even or odd
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number_to_test, remainder;
NSLog (@”Enter your number to be tested: “);
scanf (“%i”, &number_to_test);
remainder = number_to_test % 2;
if ( remainder == 0 )
NSLog (@”The number is even.”);
if ( remainder != 0 )
NSLog (@”The number is odd.”);
[pool drain];
return 0;
}

if –else loop

// Program to determine if a number is even or odd
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number_to_test, remainder;
NSLog (@”Enter your number to be tested: “);
scanf (“%i”, &number_to_test);
remainder = number_to_test % 2;
if ( remainder == 0 )
NSLog (@”The number is even.”);
else
NSLog (@”The number is odd.”);
[pool drain];
return 0;
}

if ( expression 1 )
program statement 1
else
if ( expression 2 )
program statement 2
else
program statement 3

 

// This program determines if a year is a leap year
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int year, rem_4, rem_100, rem_400;
NSLog (@”Enter the year to be tested: “);
scanf (“%i”, &year);
rem_4 = year % 4;
rem_100 = year % 100;
rem_400 = year % 400;
if ( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0 )
NSLog (@”It’s a leap year.”);
else
NSLog (@”Nope, it’s not a leap year.”);
[pool drain];
return 0;
}

 

//Output


Enter the year to be tested:
1955
Nope, it’s not a leap year.
The if Statement 109
Program 6.5 Output (Rerun)
Enter the year to be tested:
2000
It’s a leap year.
Program 6.5 Output (Rerun)
Enter the year to be tested:
1800
Nope, it’s not a leap year.

Nested if Statements

// This program categorizes a single character
// that is entered from the keyboard
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
char c;
NSLog (@”Enter a single character:”);
scanf (“%c”, &c);
if ( (c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c <= ‘Z’) )
NSLog (@”It’s an alphabetic character.”);
else if ( c >= ‘0’ && c <= ‘9’ )
NSLog (@”It’s a digit.”);
else
NSLog (@”It’s a special character.”);
[pool drain];
return 0;
}

The switch Statement

// Program to evaluate simple expressions of the form
// value operator value
#import <Foundation/Foundation.h>
// Insert interface and implementation sections for
// Calculator class here
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
double value1, value2;
char operator;
Calculator *deskCalc = [[Calculator alloc] init];
NSLog (@”Type in your expression.”);
scanf (“%lf %c %lf”, &value1, &operator, &value2);
[deskCalc setAccumulator: value1];
switch ( operator ) {
case ‘+’:
[deskCalc add: value2];
break;
case ‘-’:
[deskCalc subtract: value2];
break;
case ‘*’:
[deskCalc multiply: value2];
break;
case ‘/’:
[deskCalc divide: value2];
break;
default:
NSLog (@”Unknown operator.”);
break;
}
NSLog (@”%.2f”, [deskCalc accumulator]);
[deskCalc release];
[pool drain];
return 0;
}

Boolean Variables

// Program to generate a table of prime numbers
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int p, d, isPrime;
for ( p = 2; p <= 50; ++p ) {
isPrime = 1;
for ( d = 2; d < p; ++d )
if ( p % d == 0 )
isPrime = 0;
if ( isPrime != 0 )
NSLog (@”%i “, p);
}
[pool drain];
return 0;
}

The Conditional Operator

condition ? expression1 : expression2

Types: _Bool, _Complex, and _Imaginary

 

we should mention three other types in the language: _Bool,
for working with Boolean (that is, 0 or 1) values, and _Complex and _Imaginary, for working with complex and imaginary numbers, respectively

Objective-C programmers tend to use the BOOL data type instead of _Bool for working with Boolean values in their programs.This “data type” is actually not a data type unto itself, but is another name for the char data type.This is done with the language’s special
typedef keyword

Bit operator

 

Symbol Operation
& Bitwise AND
| Bitwise inclusive-OR
^ Bitwise OR
~ Ones complement
<< Left shift
>> Right shift

 

The Bitwise AND Operator

b1 b2 b1 & b2
———————————

0  0          0
0  1          0
1  0          0
1  1          1

w1 0000 0000 0001 0101 0x15
w2 0000 0000 0000 1100 & 0x0c
———————————————————————

w3 0000 0000 0000 0100 0x04

 

 

The Bitwise Inclusive-OR Operator

b1 b2 b1 | b2
—————————————————

0  0       0
0  1       1
1  0       1
1  1       1

 

w1 0000 0000 0001 1001 0x19
w2 0000 0000 0110 1010 | 0x6a
————————————————————————————

     0000 0000 0111 1011 0x7b

 

 

The Bitwise Exclusive-OR Operator ^

b1 b2 b1 ^ b2
———————————————————————
0 0          0
0 1          1
1 0          1
1 1          0

w1 0000 0000 0101 1110 0x5e
w2 0000 0000 1011 0110 ^ 0xd6
————————————————————————————————————
w3 0000 0000 1110 1000 0xe8

 

The Ones Complement Operator

b1 ~b1
————————
0 1
1 0

w1 1010 0101 0010 1111 0xa52f
~w1 0101 1010 1101 0000 0x5ab0

The Left Shift Operator

w1        ... 0000 0011 0x03
w1 << 1 ... 0000 0110 0x06

The Right Shift Operator

w1      1111 0111 0111 0111 1110 1110 0010 0010 0xF777EE22
w1 >> 1 0111 1011 1011 1011 1111 0111 0001 0001 0x7BBBF711

//bitwiseoperator

#include<Foundation/Foundation.h>
int main(int argc, char*argv[])
{
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
unsigned int w1=0xA0A0A0A0 ,w2= 0xFFFF0000,w3=0x00007777;
NSLog (@"%x %x %x",w1&w2,w1|w2,w1^w2);
NSLog (@"%x %x %x", ~w1, ~w2, ~w3);
NSLog (@"%x %x %x", w1 ^ w1, w1 & ~w2, w1 | w2 | w3);
NSLog (@"%x %x", w1 | w2 & w3, w1 | w2 & ~w3);
NSLog (@"%x %x", ~(~w1 & ~w2), ~(~w1 | ~w2));
[pool drain];
return 0;

}

//output

giripal sigh rawat@SUNIL ~/objc_prog404
$ ./obj/LogTest
2009-08-31 13:44:59.375 LogTest[3080] a0a00000 ffffa0a0 5f5fa0a0
2009-08-31 13:44:59.421 LogTest[3080] 5f5f5f5f ffff ffff8888
2009-08-31 13:44:59.421 LogTest[3080] 0 a0a0 fffff7f7
2009-08-31 13:44:59.421 LogTest[3080] a0a0a0a0 ffffa0a0
2009-08-31 13:44:59.421 LogTest[3080] ffffa0a0 a0a00000

 

source code download

Arithmetic expression and operation precedence

the basic mathematic operation

(+ – * /) + ( % )

Operator Precedence

Integer Arithmetic and the Unary Minus Operator

The Modulus Operator

 

Pages from Programming in Objective-C 2.0 (2nd Edition)-4_Page_1 Pages from Programming in Objective-C 2.0 (2nd Edition)-4_Page_2

 

 

//operator

#import <Foundation/Foundation.h>
int main(int var1,char *var2[])
{

NSAutoreleasePool *pool= [NSAutoreleasePool alloc ]init];
int a=400;
int b=200;
int c=100;
int d =10;

NSLog(@"a+b/c*d is :%i\n",(a+b/c*d));
NSLog(@"a+b-c*d is :%i\n",(a+b-c*d));

[pool drain];
}

//output

$ make
This is gnustep-make 2.2.0. Type 'make print-gnustep-make-help' for help.
Making all for tool LogTest...
Compiling file prog403.m ...
Linking tool LogTest ...

giripal sigh rawat@SUNIL ~/objc_prog403
$ ./obj/LogTest
2009-08-31 12:24:23.031 LogTest[2736] The result is -4000

source code download

The Type Cast Operator

Assignment Operators

// Implement a Calculator class
#import <Foundation/Foundation.h>
@interface Calculator: NSObject
{
double accumulator;
}
// accumulator methods
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;
// arithmetic methods
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
@end
@implementation Calculator
-(void) setAccumulator: (double) value
{

accumulator = value;
}
-(void) clear
{
accumulator = 0;
}
-(double) accumulator
{
return accumulator;
}
-(void) add: (double) value
{
accumulator += value;
}
-(void) subtract: (double) value
{
accumulator -= value;
}
-(void) multiply: (double) value
{
accumulator *= value;
}
-(void) divide: (double) value
{
accumulator /= value;
}
@end
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Calculator *myCal;
myCal = [[Calculator alloc] init];
[myCal clear];
[myCal setAccumulator: 1000.0];
[myCal add: 2000.];
[myCal divide: 150.0];
[myCal subtract: 100.0];
[myCal multiply: 50];
NSLog (@"The result is %g", [myCal accumulator]);
[myCal release];
[pool drain];
return 0;
}

//output

giripal sigh rawat@SUNIL ~/objc_prog403
$ make
This is gnustep-make 2.2.0. Type 'make print-gnustep-make-help' for help.
Making all for tool LogTest...
Compiling file prog403.m ...
Linking tool LogTest ...

giripal sigh rawat@SUNIL ~/objc_prog403
$ ./obj/LogTest
2009-08-31 12:24:23.031 LogTest[2736] The result is -4000

giripal sigh rawat@SUNIL ~/objc_prog403
$

source code download

Qualifiers: long, long long, short, unsigned, and signed

 

Pages from Programming in Objective-C 2.0 (2nd Edition)datatype_Page_1.png Pages from Programming in Objective-C 2.0 (2nd Edition)datatype_Page_2.png

 

long int numberOfPoints = 131071100L;

A constant value of type long int is formed by optionally appending the letter L (in upper or lower case) onto the end of an integer constant.

long long int maxAllowedStorage;  OR “%lli”.

 

long double US_deficit_2004=1.234e+7L; OR “%le” “%lf ” OR “%lg”

SHORT INT

In any case, you are guaranteed that the amount of space allocated for a short int will not be less than 16 bits

No way exists to explicitly write a constant of type short int in Objective-C.

unsigned int

unsigned int counter =0x00ffU or

unsigned long int counter=20000UL;

When declaring variables to be of type long int, short int, or unsigned int, you can omit the keyword int.

The signed qualifier can be used to explicitly tell the compiler that a particular variable
is a signed quantity. Its use is primarily in front of the char declaration

Type id

The id data type is used to store an object of any type. In a sense, it is a generic object type. For example, this line declares number to be a variable of type id:

id number;

 

So, the following declares a class method that returns a value
of type id:
 

+allocInit;

The id type is the basis for very important features in Objective-C know as polymorphism and dynamic binding,

Pages from Programming in Objective-C 2.0 (2nd Edition)-2dataspecifier.png

 

no source code

data type and expression

The Objective-C programming language provides three other basic data types: float, double, and char.

A variable declared to be of type float can be used for storing floating-point numbers (values containing decimal places).The double type is the same as type float, only with roughly twice the accuracy. Finally, the char data type can be used to store a single character, such as the letter a, the digit character 6, or a semicolon.

 

In Objective-C, any number, single character, or character string is known as a constant. For example, the number 58 represents a constant integer value.The string @”Programming in Objective-C is fun.\n” is an example of a constant character string object.

type int 

(base 10)

Two special formats in Objective-C enable integer constants to be expressed in a base other than decimal (base 10)

(base 8)

the integer is considered to be expressed in octal notation that is, in base 8.

example: octal constant 0177 = value 127 (1 ×64 + 7 × 8 + 7)An integer value can be displayed in octal notation by using the  format characters  %o in the format string of an NSLog call.

(base 16)

If an integer constant is preceded by a 0 and a letter x (either lower case or uppercase), the value is considered to be expressed in hexadecimal (base 16) notation.

example: rgbColor = 0xFFEF0D;         

NSLog (“Color is %#x\n”, rgbColor);

 

 

Type float

Floating-point constants can also be expressed in so-called scientific notation

You can use a variable declared to be of type float to store values containing decimal places.

2.25 × 10-3 or 0.00225 or 2.25e-3

scientific notation, the format characters %e should be specified  The format characters %g can be used to let NSLog decide
whether to display the floating-point value in normal floating-point notation or in scientific notation.

This decision is based on the value of the exponent: If it’s less than –4 or greater than 5 %e (scientific notation) format is used; otherwise, %f format is used.

Type double

The type double is similar to the type float, but it is used whenever the range provided by a float variable is not sufficient.Variables declared to be of type double can store roughly twice as many significant digits as can a variable of type float. Most computers
represent double values using 64 bits.

Type char

A character constant is formed by enclosing the character within a pair of single quotation marks. So ’a’, ’;’, and ’0’ are all valid examples of character constants.

//program to store and print value

 

#import  <Foundation/Foundation.h>

int main(int argc ,char *argv[])
{
NSAutoreleasePool * pool =[[NSAutoreleasePool alloc] init];

int intVar=101;
float floatVar=234.56;
double doubleVar=8.44e6;
char charVar ='w';

NSLog (@"integer value is %i",intVar );

NSLog (@"float value is %f", floatVar);

NSLog (@"double value is %e",doubleVar );

NSLog (@"double value is %g",doubleVar );

NSLog (@"char value is %c",charVar );

return 0;
[pool drain];
}

GNUmakefile

include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME=LogTest
LogTest_OBJC_FILES=prog401.m
include $(GNUSTEP_MAKEFILES)/tool.make

Output

Fullscreen capture 8312009 94802 AM.bmp

source code download

Sunday, August 30, 2009

working with different file

 

we are going to use three different file

 

Fraction.h for creating interface

Fraction.m for creating implementation

FractionTest.m  contain main method to implement the Fraction interface

######################################################

Fraction.m

######################################################

//
// Fraction.h
// FractionTest
//
// Created by Steve Kochan on 7/5/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
// The Fraction class
@interface Fraction : NSObject
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
-(double) convertToNum;
@end

 

 

######################################################

fraction.m

######################################################

//
// Fraction.m
// FractionTest
//
// Created by Steve Kochan on 7/5/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
-(void) setDenominator: (int) d
{
denominator = d;
}
-(int) numerator
{
return numerator;
}
-(int) denominator
{
return denominator;
}
-(double) convertToNum
{

if (denominator != 0)
return (double) numerator / denominator;
else
return 1.0;
}
@end

######################################################

FractionTest.m

#import "fraction.h"
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFraction = [[Fraction alloc] init];
// set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];
// display the fraction
NSLog (@"The value of myFraction is:");
[myFraction print];
[myFraction release];
[pool drain];
return 0;
}

 

######################################################

GNUmakefile

######################################################

include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME = LogTest
LogTest_OBJC_FILES = Fraction.m  FractionTest.m
include $(GNUSTEP_MAKEFILES)/tool.make

***************************************************************************

//to run in MAC Ox

gcc –framework Foundation Fraction.m FractionTest.m –o FractionTest

***************************************************************************

 

source code download

working with class and creating object

working with classes  wrong in objective c we are working with interface that is similarly like classes in  java and c++

*************************************************************************

//creating a class sorry! interface implementation and object

 

#import <Foundation/Foundation.h>
//---- @interface section ----
@interface Fraction: NSObject
{
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
@end

//---- @implementation section ----

@implementation Fraction
-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}
-(void) setNumerator: (int) n
{
numerator = n;
}
-(void) setDenominator: (int) d
{
denominator = d;
}

@end
//---- program section ----

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFraction;
// Create an instance of a Fraction
myFraction = [Fraction alloc];
myFraction = [myFraction init];
// Set fraction to 1/3
[myFraction setNumerator: 1];
[myFraction setDenominator: 3];
// Display the fraction using the print method
NSLog (@"The value of myFraction is:");
[myFraction print];
[myFraction release];
[pool drain];
return 0;
}

*******************************************************************************

creating file as  prog301.m using vi or ascii text editor

create anather file called   GNUmakefile without extension

##########################################

include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME = LogTest
LogTest_OBJC_FILES = prog301.m
include $(GNUSTEP_MAKEFILES)/tool.make

############################################

LogTest_OBJC_FILES = prog301.m specify the file that is to be compiled.

compile the file

$ make

running the command

$ ./obj/LogTest

 

sourcecode:

  http://sites.google.com/site/phoneprogramming/objc_prog301.zip

running and compiling in windows dos based

 

step 1 : installing software  list

GNUstep System Required       link:  0.23.0

GNUstep Core     Required       link:  0.23.1

i will be telling about the basic and minimum step required for compiling and running ur objective-c program

first of all

download  the  software and install the in the directory specified 

the default directory is c:\GNUstep

Fullscreen capture 8302009 52100 PM

 

run the shell from start>all program >

Fullscreen capture 8302009 52604 PM.bmp

 

this will run a dos shell  prompt the the shell will only  run linux or unix  shell command only.

 

Fullscreen capture 8302009 53250 PM.bmp

this will be our window environment for development objective –c program

step 2 writing a simple program

$ vi programname.m

option for vi i  insert, esq+shift+: open the command prompt  s=save and q =quit u can use both or any one combination

OR

write a program in ascii test editor  and save that program under the directory C:\GNUstep\home\”username folder”\

*********************************************************

// First program example

#import <Foundation/Foundation.h>

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

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

NSLog (@"Programming is fun!");

[pool drain];

return 0;

}

********************************************************

step 3 for compiling there is some step to follow

create a

filename GNUmakefile. with these for line

 

 

*********************************************************
include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME = LogTest
LogTest_OBJC_FILES = source.m
include $(GNUSTEP_MAKEFILES)/tool.make

*********************************************************

save the file in present program directory

for compiling run command

$ make

 

for running compiled program

$ ./obj/LogTest

Fullscreen capture 8302009 55435 PM.bmp

 

 

source code download http://sites.google.com/site/phoneprogramming/objc_prog201.zip

Compiling and running


 


 

Using Xcode

Xcode is a sophisticated application that enables you to easily type in, compile, debug, and execute programs. If you plan on doing serious application development on the Mac,


 

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

Figure 2.1shows its icon.

Pages from Programming in Objective-C 2.0 (2nd Edition)_img_0 

Start Xcode. Under the File menu, select New Project


 

A window appears, as shown in Figure 2.3.

Pages from Programming in Objective-C 2.0 (2nd Edition)_img_1


 


 


 

Scroll down the left pane until you get to Command Line Utility.In the upper-righ tpane, highlight Foundation Tool. Your window should now appear as shown


 Pages from Programming in Objective-C 2.0 (2nd Edition)_img_2

 

Click Choose.

This brings up a new window,shown

Pages from Programming in Objective-C 2.0 (2nd Edition)_img_3


 


Starting a new project: selecting the application type

Starting a new project: creating a Foundation tool

Pages from Programming in Objective-C 2.0 (2nd Edition)_img_4
 

 

first program name prog1,so type that into the Save As field.

Pages from Programming in Objective-C 2.0 (2nd Edition)_img_5
 


 

You may want to create a separate folder to store all your projects in. On my system,I keep the projects for this book in a folder called ObjC Progs.

Click the Save button to create your new project. This gives you a project window

  Pages from Programming in Objective-C 2.0 (2nd Edition)_img_6
 



 

Note that your window might display differently if you've used Xcode before or have changed any of its options.


 

Now it's time to type in your first program. Select the file prog1.m

in the upper-right pane.

Your Xcode window should now appear as shown



 


 

Objective-C source files use .m as the last two characters of the filename (known as its extension).

commonly used filename extensions.

Returning to your Xcode project window, the bottom-right side of the window shows the file called prog1.mand contains the following lines:


 

#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;

}


 


 

Note If you can't see the file's contents displayed, you might have to click and drag up the bottom right pane to get the edit window to appear. Again, this might be the case if you've previously used Xcode.

You can edit your file inside this window. Xcode has created a template file for you to use.

Don't worry about all the colors shown for your text onscreen. Xcode indicates values,

Reserved words, and so on with different colors.

Now it's time to compile and run your first program—in Xcode terminology, it's called build and run. You need to save your program first, however, by selecting Save from the File menu.

If you try to compile and run your program without first saving your file, Xcode asks whether you want to save it.


 

Compiling and Running Programs Under the Build menu, you can select either Build or Build and Run. Select the latter because that automatically runs the program if it builds without any errors. You can also click the Build and Go icon that appears in the toolbar.

Note Build and Go means "Build and then do the last thing I asked you to do," which might be Run, Debug, Run with Shark or Instruments, and so on. The first time you use this for a project, Build and Go means to build and run the program, so you should be fine using this option. However, just be aware of the distinction between "Build and Go" and "Build and Run."

If you made mistakes in your program, you'll see error messages listed during this step.

In this case, go back, fix the errors, and repeat the process. After all the errors have been removed from the program, a new window appears, labeled prog1 – Debugger Console.

This window contains the output from your program and should look


Pages from Programming in Objective-C 2.0 (2nd Edition)_img_7 



 


 


 


 


 

If this window doesn't automatically appear, go to the main menu bar and select Console from the Run menu.


 

We discuss the actual contents of the Console window shortly.

You're now done with the procedural part of compiling and running your first pro-

gram with Xcode (whew!).The following summarizes the steps involved in creating a new program with Xcode:

1.Start the Xcode application.

2.If this is a new project, select File, New Project.

3.For the type of application, select Command Line Utility, Foundation Tool, and click Choose.

Select a name for your project, and optionally a directory to store your project files in. Click Save.

5.In the top-right pane, you will see the file prog1.m(or whatever name you assigned to your project, followed by .m. Highlight that file. Type your program into the edit window that appears directly below that pane.

6.Save the changes you've entered by selecting File, Save.

7.Build and run your application by selecting Build, Build and Run, or by clicking the Build and Go Button.

8.If you get any compiler errors or the output is not what you expected, make your changes to the program and repeat steps 6 and 7.


 


 


 

Using Terminal


 Pages from Programming in Objective-C 2.0 (2nd Edition)_img_8


 

Some people might want to avoid having to learn Xcode to get started programming with Objective-C. If you're used to using the UNIX shell and command-line tools, you might want to edit, compile, and run your programs using the Terminal application. Here we examine how to go about doing that.

The first step is to start the Terminal application on your Mac. The Terminal application is located in the Applications folder, stored under Utilities. Figure 2.9shows its icon.

Start the Terminal application. You'll see a window that looks like Figure 2.10.

You type commands after the


 

$


 

depending on how your Terminal application is configured) on each line. If you're familiar with using UNIX, you'll find this straight forward.

First, you need to enter the lines from Program


 


 


 


 


 

into a file. You can begin by creating a directory in which to store your program examples. Then you must run a text editor, such as vi or emacs, to enter your program:


 

$mkdir Progs


 

Create a directory to store programs in


 

$cd Progs


 

Change to the new directory


 

$vi prog1.m


 

Start up a text editor to enter program..


 


 

NoteIn the previous example and throughout the remainder of this text, commands that you, then user, enter are indicated in boldface.

For Objective-C files, you can choose any name you want;

just make sure the last two characters are .m.

This indicates to the compiler that you have an Objective-C program.

After you've entered your program into a file, you can use the GNU Objective-Ccompiler, which is called gcc, to compile and link your program.This is the general format of the


 


 

gcc command:gcc –framework Foundation files-o progname


 

This option says to use information about the Foundation framework:

-framework Foundation


 

Just remember to use this option on your command line.

Files is the list of files to be compiled. In our example, we have only one such file, and we're calling it prog1.m.

Prog name is the name of the file that will contain the executable if the program compiles without any errors.


 

We'll call the program prog1;here,then,is the command line to compile your first Objective-C program:


 

$gcc –framework Foundation prog1.m -o prog1


 

Compile prog1.m & call it prog1


 

The return of the command prompt without any messages means that no errors were found in the program. Now you can subsequently execute the program by typing thename prog1 at the command prompt:


 

$prog1


 

Execute prog1


 

prog1: command not found


 


 

This is the result you'll probably get unless you've used Terminal before.


 


 

The UNIX shell (which is the application running your program) doesn't know where prog1is located (we don't go into all the details of this here),

so you have two options: One is to precede the name of the program with the characters ./ so that the shell knows to look in the current directory for the program to execute. The other is to add the directory in which your programs are stored (or just simply the current directory) to the shell's PATH variable. Let's take the first approach here:


 


 

$ ./prog1


 

Execute prog1


 

2008-06-08 18:48:44.210 prog1[7985:10b] Programming is fun!

Pages from Programming in Objective-C 2.0 (2nd Edition)_img_9
 



 

You should note that writing and debugging Objective-C programs from the terminal is a valid approach. However,it's not a good long-term strategy.


 

If you want to build MacOS X or iPhone applications,


 

there's more to just the executable file that needs to be "packaged" into an application bundle. It's not easy to do that from the Terminal

application, and it's one of Xcode's specialties. Therefore, I suggest you start learning to use Xcode to develop your programs. There is a learning curve to do this, but the effort will be well worth it in the end.

Saturday, August 29, 2009

ur first basic program

// First program example


#import Foundation/Foundation.h> //header file


int main (int argc, const char * argv[]) //main program
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // memory management
NSLog (@”Programming is fun!”); //print statement
[pool drain]; //release memory
return 0; //return type
}


how to compile :
***********************************************************************
You can both compile and run your program using
Xcode, or you can use the GNU Objective-C compiler in a Terminal window. on Mac os

These tools should be preinstalled on all Macs that came with OS X.
If you separately installedOS X, make sure you install the Developer Tools as well.


and u can even compile under windows also.