Objective C Programming Language – Beginners Guide

October 9, 2018
Mubashir

The objective c is general- purpose, object oriented programming language that was used to add Smalltalk style and messaging to the c language. It was the actual language used by apple for OSX and IOS operating system. It is also used their respective application programming interfaces cocoa and cocoa touch prior to the introduction of swift language. Objective c is the superset of c programming language, designed to be object oriented and general purpose language. The objective c is primary language for OS and X and IOS.

objective C programming language

Objective C Programming Language – Beginners Guide

The objective c language originally developed in early 1980s.It was used as main language used by NEXT for their NEXTSTEP operating system, from which OSX and IOS are derived. Portable objective c programs that did not use cocoa and cocoa touch libraries or those using part that may be ported or re-implemented for the system. This can be compiled for any system supported by GNU compiler construction or CLAG. Objective c programming language file have the .m file name extension while objective c hider file has the .h file name extension as the same as c files.

The objective c++ file have the extension of .mm file name file extension. The development tool for running, debugging and to execute the objective c language is X-code.

Ranking of Objective C Programming Language:

The Objective C  Programming Language is superset of c language that’s mean we can run ,compile the program written in C language by using Objective C compiler. Objective C is a primary language for OSX and IOS.

According to the IEEE spectrum the of Objective C Language holds 26 position in ranking.but now a days, the C language is the top which is the supper set of Objective C.

History of Objective C Programming language:

The objective c was created initially by Brad cox and Tom love in the early 1980s at their company Stepstone. Both had been introduced to Smalltalk while at ITT corporations programming technology center in 1981. The earliest work on objective c traces back to around that time. The cox was intrigued by problem of true reusability in software design and programming. He realizes that a language like Smalltalk would be invaluable in building development environments at system development at ITT. However, he and tom love also recognized that backward compatibility with c was critically important in ITT, s telecom engineering milieu.

Cox began writing a preprocessor of c to add some abilities of Smalltalk. he soon had a working implementation of an object oriented extension to the c language which he called “OOPC ” for object oriented pre –processor . Love was hired by schlumberger research in 1982 and had the opportunity to acquire the first commercial copy of Smalltalk- 80 which further influenced the development of their brainchild. Love and cox eventually formed a new venture, productivity products international (PPI) to commercialize their products, which coupled an Objective C compiler with class libraries.

In 1986 cox published the main description of the objective –c language in its original form book object oriented programming, an evolutionary approach. Although he was careful to point out that there is more to the problem of reusability than just the problem, objective –c often found itself compares feature with the feature of other language.

Popularization through the Next:

In 1988 Next licensed objective –c  from step stone and extended the GCC compiler to support the objective –c .  Next develop the appkit and foundation kit libraries on which the NEXTSTEP user interface and interface builder were based . while the Next station failed to impact the market place ,the tools were widely lauded  in the industry. This lid  NEXT to drop hardware production and focus on software tools, selling   NEXTSTEP as a platform for custom programming . In order to circumstances the next had originally intended to ship the objective- c  fomented separately ,allowing the user to link it with GCC to make the compiler executable ,after being initially accepted by Richard  M. Stallman this plan was rejected after Richard consulted with GNU,s  lawyered and Next agreed to make objective –c  part of GCC.

GNU start work on its free software implementation of cocoa ,which was called GNU setup based on open step standard . The Dennis Glutting  wrote his first GNU objective –c runtime in 1992.which had been used till 1993.

Apple development and Swift:

After the invent of the NEXT in 1996, apple computer used open step in its new operating system, OS X.  this included the objective –c , Next objective c  based  developers tools, project builder and its interface tools ,interface builder ,now both are merged in one tool called x code  .

Most of the apple current cocoa API is based on open step interface objects and its most important  objective –c environment used  for active development.

USES OF OBJECTIVE C:

Objective c is a primary programing language we use when writing software for OS X and IOS .its is superset of c language and provide object oriented language  facilities  and dynamic runtime .

The objective c language was chosen for cocoa framework for a variety of reasons. The first reason Is that it is object oriented language .the kind of functionalities that packed in cocoa framework can only be delivered by using object oriented language. Second , because objective c is an extension of standard ANSI C, existing c program can be adopted to use software framework without losing any work that that went into their original development.

Moreover , objective is simple language , its syntax is simple ,easy to understand.

Development tools for Objective C Programming Language:

There are following tools associated with development of objective c

  1. COCOA API Is based open step interface, and is the most significance objective c environment being used for active development.
  2. Project Builder Is an integrated development tool developed by   NEXT for the NEXTSTEP operating system.
  3. X-CODE is integrated development environment constructed by apple.

Compiling and Running 1st Program:

We can use both for compiling and running objective c program X-CODE or by using GNU objective –c compiler in a terminal window .we will use x code here, X code is sophisticated application for easily compiling and execution of the objective c language code.

1. Download the x-code from apple store using apple id and install the x-code and start doing work.

2. Start x code. you can then select “new project” from menu bar

3. A new window appears.

4. Click next and give name to your new project, which will contain file related to your project. here you can specify where you can store your folder.

5. Click save to your project to create your new project .x code will appear a new project window .

Now it’s time to type to your new program .select the main .m  in the left panel. Now compile and run the program. the  middle icon will show the output of your program as.

Here is first program.

Common file name Extension in the Objective C:

Extension                    Meanings

.c                                   is the   c language source file

.cc ,.cpp                         is the   c++  source file

.h                                   .h is the  header file

.m                                   objective –c language  source file

.mm                                the objective –c ++ source file

.pl                                   is the Perl source file

Explanation of Your First Program:

// main.m

// prog1

//

// Created by Steve Kochan on 1/30/11.

// Copyright 2011 ClassroomM, Inc.. All rights reserved.

//

#import <Foundation/Foundation.h>

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

{

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

NSLog (@”Programming is fun!”);

[pool drain];

return 0;

}

Explanation:

In objective c language upper and lower case letters are distinct.it does not care where you star the line. Comments can be add into the program by two ways once by using by using two consecutive slashes characters (//) compiler ignore the slashes understand the end of the line. Second you can add comments by using the two characters /and* for start and *and /for the end of comments.

Programs in Objective C:

No 1 :-  write a program which add two number and display sum of that numbers

#import <Foundation/Foundation .h>

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

{

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

int value1, value2, sum;

value1 = 50;

value2 = 25;

sum = value1 + value2;

NSLog (@”The sum of %i and %i is %i”, value1, value2, sum);

[pool drain];

return 0;

}

Output:

The sum of 50 and 25 is 75.

Variables in Objective C:

The variable is  name given to an area of storage that our program can manipulate . each variable in objective c had a specific type ,which specifies the size and layout of the memory ,range of the values that can be stored memory and set of operations that can be applied to memory

The name of variable consist  of the  letters ,digits and underscore characters .

Type            Description

Char       typically a single a single

octet       this is an integer type

Int          the  natural  most size of an integer for the machine

Float      a single precision  floating point value

Double   a double precision floating point value

Void       represent the absent of type

Variable defining in Objective C:

A variable definition specifies a data type and contains one or more variable of that data type as follows

Type variable list

Here, type must be a valid Objective-C data type including char, w_char, ints, float, double, bool or any user-defined object, etc., and variable_list may consist of one or more identifier names separated by commas. Some valid declarations are shown here:

Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows:

type variable name = value;

Example of variable in Objective C:

#import <Foundation/Foundation.h>

// Variable declaration:

extern int a, b;

extern int c;

extern float f;

int main ()

{

/* variable definition: */

int a, b;

int c;

float f;

/* actual initialization */

a = 10;

b = 20;

c = a + b;

NSLog(@”value of c : %d \n”, c);

f = 70.0/3.0;

NSLog(@”value of f : %f \n”, f);

return 0;

}

Output:

2013-09-07 22:43:31.695 demo[14019] value of c : 30

2013-09-07 22:43:31.695 demo[14019] value of f : 23.333334

Loops in Objective C Programming Language:

In Objective C you can execute the programme in several ways by using loops.

Following loops are used in objective c :-

  1. The for statement
  2. The while statement
  3. The do while statement

1. The for statement

Here we give an example of for loop to calculate 200th triangular numbers.

// Program to calculate the 200th triangular number

// Introduction of the for statement

#import <Foundation/Foundation.h>

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

{

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

int n, triangularNumber;

triangularNumber = 0;

for ( n = 1; n <= 200; n = n + 1 )

triangularNumber += n;

NSLog (@”The 200th triangular number is %i”, triangularNumber);

[pool drain];

return 0;

}

Output:

The 200th triangular number is 20100

2. The while statement

The while statement extend the objective c language

The syntax of while loop as follows :-

While (expression ): Program statement

The expression specified inside the parentheses is evaluated, if the result of the expression is true then execute the program statement .the program statement execute until the expression remains true

Example of the while statement :-

// This program introduces the while statement

#import <Foundation/Foundation.h>

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

{

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

int count = 1;

while ( count <= 5 ) {

NSLog (@”%i”, count);

++count;

}

[pool drain];

return 0;

}

Output :

1

2

3

4

5

(Explain it self)

3. Do statement:

The do statement is differ from above two lops .

In above for ,and while statement the condition is first checked and then body of the program execute

In this case the condition is different .in do while statement the program statement is execute first and condition is checked latter .

The syntax of the do while is follows :-

Do

Program statement

While (expression);

Example of the do while statement:

// 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 .

135

5

3

1

Break statement:

Sometimes we wants to stop the program execution  after a certain time ,and wants to stop the loop execution  ,you can use the break statement to  do this

The continuous statement:

The continuous statement is similar to the break statement exception it does not stop the loop execution ,after the break statement it will skip all the statements.

Classes in Objective C Programming Language:

What is an object ?

The object is thing . consider an example of daily life a person has a car which is an object and he do something thing with it . in object oriented aspects, your particular car is the instance of the car . here the name of the class is car from which this instance that was created .

Action and object:

Object              action

(Your is  car)     (drive it, Wash it, Fill it with gas, Service it )

Instance and methods:-

The unique occurrence of the class is the instance. And the actions performed by object on the instance called method . The object are unique representation of the class , each object contains some specific information . and methods provides the means of data accessing and changing that data.

Program of the fraction class :-

Here we give an example of the class program name of the class is fraction ,in this program we will calculate the fraction of the two number  .

// Program to work with fractions – class version

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

}

Output:

Value of the fraction is

1/3

No comments

Leave a Reply