Objective C tutorial for beginners

objectivectutorialforbeginnersObjective C is an object-oriented language, based on the C programming language with an addition of ‘Small Talk’ programming extensions. It was developed by ‘NeXT’ and then was shortly adapted by Apple for the development of some core parts of ‘IOS’ and ‘MAC OS X’ operating systems. Objective C is also used to develop the ‘Cocoa’ and ‘Cocoa Touch’ API’s as well as ‘MAC OS X’ and ‘IOS’ applications developed in those platforms. Since ‘Objective C’ is a superset of C language, it is easy to use ‘C’ and even ‘C++’ in same ‘Cocoa’ applications that use Objective C.

New to Objective C? Follow this video course at Udemy.com

Hello World Program in Objective C

The following example demonstrates basic concept of the code structure in ‘Objective C’ along with some commonly used keywords.

Code snippet:

#import <Foundation/Foundation.h>

@interface HelloWorldExample: NSObject

- (void) printMethod;

@end

 

@implementation HelloWorldExample

- (void) printMethod {

NSLog (@"Hello World \n");

}

@end

 

int main ()

{

/* Main method a starting point

in every Objective-C programs */

HelloWorldExample * HelloWorldExample = [[HelloWorldExample alloc] init];

// Method call

[HelloWorldExample printMethod];

return 0;

}

The above ‘Objective C’ code snippet prints out a ‘Hello World’ on a console. The first line in the example ‘#import <Foundation/Foundation.h>’ is a preprocessor command that tells the compiler to include a specified file before instantiating code.

The ‘@interface’ directive creates a new class. It inherits from ‘NSObject’, which is the super class of all the objects in ‘Objective C’ and provides a basic class method ‘+alloc’ and instance ‘-init’ method to allocate a memory for the class objects. The next line of the code declares the method ‘printMethod’ and ‘@end’ shows an end of that class. The ‘@implementation’ directive provides an application of that class, which includes the implementation of methods declared in ‘@interface’.

The ‘main’ method is the starting point of every ‘Objective C’ program. In the above code, this method is used to creates the object  ‘HelloWorldExample’ by allocating memory and initializing it. Finally, a method of that class is called to print out the output using ‘NSLog’ and  the ‘main’ method is then terminated with a line of ‘returns 0’.

For more interesting Objective C tutorials, take this course at Udemy.com

Objective C Basics

Following are some of the important points that must be kept in mind while developing ‘Objective C’ applications.

  • In ‘Objective C’ each statement is terminated with a semi-colon.
  • Comments in ‘Objective C’ are similar to those in C language and some other languages. One line comment starts with a ‘//’ and multiple line comment starts with a ‘/*’ and ends on ‘*/’ as shown in above code snippet of ‘Hello World’ example.
  • The whitespace separates different elements in a statement aiding ‘Objective C’ compiler identify the keywords, identifiers and other code components. For instance in the following code of line, the whitespace distinguishes the data type integer and the variableName.

int variableName;

  •  Whitespace in ‘Objective C’ is also helps improve code readability. For instance in the following lines of code, there is no need for whitespace, but it helps in a readability of a code.

cars = bmw + porche;

 

wheelTypes = inch17s +inch19s;

Objective C Data Types, Variables and Operators

In the ‘Objective C’ programming language, data types are used for declaring variables or functions with different types. The type of a variable determines how much space it will take in a memory, and in the function it specifies the return type. Variables are the containers with names that are used to hold data based on its data type. It is composed of letters, digits and an underscore (_) character.  But it cannot start with any digits and cannot include spaces. Objective C is case sensitive, so variables with the same name but different case are considered two separate variables.

The following are the common C language types that are used in ‘Objective C’.

Data TypesDescription
intInteger number including both positive and negatives.
unsigned intInteger number includes only positive.
float, doubleFloating point decimal numbers with a single and double precision respectively.
charSingle character.
boolBoolean values.

 

Variable declaration tells the compiler to assign a specific amount of memory based on its data type. The following are some of the variable declaration examples in ‘Objective C’.

int var1, _var2 = 22, var_3 = 1;

char singleCharacter;

float salary;

double comPercentage;

In ‘objective C’, the arithmetic operations on the variables or constant (fixed values) are carried out in the same way as in the C language using different ‘Operators’. This follows standard math operator precedence; therefore multiplication, division, module, addition and subtraction is a priority order from high to low. The following are the common operators.

OperatorDescription
+Addition
Subtraction
*Multiplication
/Division
%Module

 

OperatorDescriptionSyntax
++IncrementVar1++: first used then an increment by one.++Var1: first increment then used.
DecrementVar1++: first used then a decrement by one.++Var1: first decrement then used.

 

In ‘Objective C’ assignment operators are used to shorten the statement of increment, decrement, multiplication and division. Like ‘a += 10’ is equitant to ‘a = a +10’.

The following code snippet illustrates the basic use of ‘Objective C’ data types, variable and operator described in this section.

Code snippet:

// variable definition:

int var1, var2;

int resultVar;

float floatPointVar;

// variable initialization

var1 = 10; var2 = 10;

// + operator

resultVar = var1 + var2;

NSLog (@"value of resultVar : %d \n", resultVar); // 20

//post increment operator

NSLog (@"value of resultVar : %d \n", resultVar++); // 20

 

//pre increment operator

NSLog (@"value of resultVar : %d \n", ++resultVar); // 22

//assignment operator

resultVar += 10;

NSLog (@"value of resultVar : %d \n", resultVar); //32

// Flosting point division

floatPointVar = 72.0/resultVar;

NSLog (@"value of floatPointVar : %f \n", floatPointVar); // 2.250000

 

Objective C is not a difficult language to learn. It’s based on the C language. Anyone with a background in C can easily adapt to Objective C. Furthermore, in the current era where mobile applications are gaining popularity, learning Objective C can be helpful towards landing a decent job.

Udemy.com contains several tutorials for beginners in Objective C, one such tutorial can be found here