iOS For Beginner

Objective C Fundamental

In this chapter we fully concentrate on Objective C language with some more important concepts to build iOS application. When we build apps for OS X or iOS, we spend most of our time working with objects. Those objects are instances of Objective ­C classes, some of which are provided for us by Cocoa or Cocoa Touch and some of which we write ourself.


A class describes the behaviour and properties common to any particular type of object. For a string object (in Objective­C, this is an instance of the class NSString), the class offers various ways to examine and convert the internal characters that it represents.


@interface SimpleClass : NSObject

In this above syntax, class name “SimpleClass” are inherits from NSObject.

Properties to Object value :

Objects often have properties intended for public access. Look at the below example code.

@interface people : NSObject

@property NSString * oldpeople;
@property NSString * youngpeople;


In this example, the people class declares two public properties, both of which are instances of the NSString class.

Both these properties are for Objective­C objects, so they use an asterisk to indicate that they are C pointers. They are also statements just like any other variable declaration in C, and therefore require a semicolon at the end.

Define and declaration :

In Objective C, we have two files viz. .h,.m. Where the declaration will be done in .h. And define class will be done in .m. Simply .h is an interface file. .m is an implementation file. For example :

A simple interface file = Diary.h

@interface diaryclass : NSObject {
//declare variable

//declare property,method


A simple implementation file = Diary.m

@implementation diaryclass

// define the class methods


so these are the two files which involves in objective ­ c. Now lets see some of the cool stuff to create object, instance classes, property, method.

Diaryclass *objectName = [[Diaryclass alloc] init]; // create an object

(returnType)methodname:(typename)variable:(typeName)variable1; //method declaration

for example:


@property(nonatomic , strong) NSString *myName; // property
self.myName = @”Spike”; //Access the property

Header Field :
To reference class header files, Objective C uses the #import directive. Always use this instead of the #include coming from C, also if you are importing C files, because #import will check automatically for double inclusion of headers so you will never have problems with recursive inclusion. #import uses angular brackets < > for global inclusions and double quotation marks ” “ for local ones. So if you are including a framework you do it like this:


Memory Management :

Application memory management is the process of allocating memory during your program’s runtime, using it, and freeing it when you are done with it. A well written program uses as little memory as possible. In Objective­C, it can also be seen as a way of distributing ownership of limited memory resources among many pieces of data and code. When you have finished working through this guide, you will have the knowledge you need to manage your application’s memory by explicitly managing the life cycle of objects and freeing them when they are no longer needed.

Memory Management Tool :

We can analyse the usage of memory with the help of xcode tool instrument. It include tools such as Activity monitor, instrument, leaks so on.


In Objective ­ c , we using two array classes.NSArray and NSMutableArray.For example :

NSArray *arryname = [[NSArray alloc] initWithObjects:@”array1”, nil]; // NSArray
NSMutableArray *array = [[NSMutableArray alloc] init]; //NSMutableArray
[array addobject:@”Array1”]; // Add object to NSMutable array


In Objective C , we using two array classes.NSDictionary and NSMutableDictionary.For example :

NSDictionary *dict = [[NSDictionary alloc] initWithObject:[NSArray arrayWithObjects:@”array1”,nil] for keys:[NSArray arrayWithObjects:@”Key1”]]; // NSDictionary

NSMutableDictionary *Mdict = [[NSMutableArray alloc]init]; //NSMutableDictionary
[Mdict setObject:@”array1” for key:@”key1”];

Basic C Primitive Types in Objective ­ c:

int A = 88;
A++ // 89
A*= 2 //178
float floatnumber = 2.13
double value = 9.8846484

In Objective ­ c, it also offer literal syntax for dictionary creation :

NSDictionary *dict = @ {

@”object” : someobject, @”string” : @”stringname”, @”number” : @”22”


Xib :

For files with .xib extension, they’re Interface Builder files that store the application’s user interface (UI). As you click on the .xib file, Xcode automatically switches to the Interface Builder for you to edit the UI of the app via drag­and­drop.

Interface Builder Xcode

Interface Builder Xcode

Comments :

Comments in objective ­ c are should be like below. If you want to comment many code you do something link this:

// comment line

/* NSMutableDictionary *Mdict = [[NSMutableArray alloc]init]; //NSMutableDictionary

[Mdict setObject:@”array1” for key:@”key1”]; */


The syntax to define a block literal used by caret symbol (^) :

^ {

// this is block


These are the most common types of C used in Objective ­C programs.

int integers number, + ­ 0,14, ­55­
unsigned int positive integers 23,5
float,double floating point decimal number 0.78 , ­21.98
bool boolean value true, false
char single text character ‘A’, ‘!’

Dealing with errors :

This is important concepts involved in objective ­ c or iOS Development. When you start creating iOS application , you need to learn more about how to handle the errors too.When you’re writing code with Objective­C, exceptions are used for programmer errors, like out­of­bounds array access or invalid method arguments. These are the problems that you should find and fix during testing before you ship your app.All other errors are represented by instances of the NSError class. This topic gives a brief introduction to using NSError objects, including how to work with framework methods that may fail and return errors.

If you need to request data from a remote web service, for example, there are a variety of potential problems that may arise, including:

1. No network connectivity
2. The remote web service may be inaccessible
3. The remote web service may not be able to serve the information you request The data you receive may not match what you were expecting

Use delegate method to alert Error:

If you’re implementing a delegate object for use with a framework class that performs a certain task, like downloading information from a remote web service, you’ll typically find that

you need to implement at least one error­related method. The NSURLConnectionDelegate protocol, for example, includes a connection:didFailWithError: method:

(void)connection:(NSURLConnection *)connection didFailWithError :(NSError *)error;

Exception for errors:

@try {

// code here


@catch (NSException *exception) {

// your code


@finally { // your code


If an exception is thrown by the code inside the @try block, it will be caught by the @catch block so that you can deal with it. If you’re working with a low-level C++ library that uses exceptions for error handling, for example, you might catch its exceptions and generate suitable NSError objects to display to the user.

Please login to get access to the quiz
Basics of Swift & Objective C (Prev Lesson)
(Next Lesson) Swift Language
Back to iOS For Beginner
Inquire Now
close slider