Introduction to Objective C
Objective C
•  Objective C is a programming language, which is used by Apple for
developing the application for iPhone and Mac Systems.
•  Objective C is very old programming language and it was designed and
developed in 1986. Now Objective C has become popular once again as it is
being used by Apple to develop applications for Mac system and iPhone.
•  Full superset of C language
•  Allows any traditional C code.
•  Adds powerful Object oriented capabilities.
OverView
•  Objective C consists of objects, classes, instance variables,
methods.
•  Built entirely around objects.
•  Objects like Windows, views, buttons, controllers exchange
information with each other, respond to events, pass actions to run a
program.
•  In C we write .c and .h files, here we write .h and .m files.
•  .h are the header files and .m are the source code or implementation
files.
Objective C Language
• 
• 
• 
• 
• 

Keywords
Message
Classes and method declaration
Instance Methods and Class Methods
Constructors

•  User Defined Constructors

•  Categories
•  Protocols
Keywords
Keywords in objective C has a prefix @ appended to them. We will look at the
keywords used for different purposes in this section
Keyword

Definition

@interface

This is used to declare a class/interface

@implementation

This is used to define class/category

@protocol

This is used to declare a protocol
Keywords Cont…
Interface
The declaration of a class interface begins with the compiler directive
@interface and ends with the directive @end.
@interface ClassName : ItsSuperclass
{
instance variable declarations
}
method declarations
@end

the name of the interface file usually has the .h extension typical of header
files.
Keywords Cont…
Implementation
The definition of a class is structured very much like its declaration. It
begins with the @implementation directive and ends with the @end directive
@implementation ClassName : ItsSuperclass
method definitions
@end

The name of the implementation file has the .m extension, indicating that it
contains Objective-C source code.
Keywords cont..
Next are the access modifiers. They decide the visibility/ scope of the instance
variables/methods
Keyword

Definition

@private

The instance variable is accessible only within the class that declares it.

@public

The instance variable is accessible everywhere

@protected

The instance variable is accessible within the class that declares it and within
classes that inherit it.
Keywords Cont…
Other keywords:

Keyword

Description

@class

Declares the names of classes
defined elsewhere.

@”string”

Defines a constant NSString object
in the current module and initializes
the object with the specified string.

@property

Provides additional information
about how the accessor methods
are implemented

@synthesize

Tells the compiler to create the
access or method(s)
Keywords Cont…
Declaring a simple property
@interface MyClass : NSObject {
float value;
}
@property float value;
@end
A property declaration is equivalent to declaring two accessor methods i.e.
-(float)value;
-(void)setValue:(float)newValue;
These methods are not shown in the code but can be overridden.
Keywords Cont…
Synthesizing a property with @synthesize
@implementation MyClass : NSObject
@synthesize value;
@end

When a property is synthesized two accessor methods are generated
i.e.
-(float)value;
-(void)setValue:(float)newValue;
These methods are not shown in the code but they can be overridden.
Keywords Cont…
Self
l 

Self is a keyword which refers to current class.

{
[self setOrigin:someX :someY];
}

In the example above, it would begin with the class of the object receiving
the reposition message.
Keywords Cont…
super
l 

l 

It begins in the superclass of the class that defines the method where
super appears.
Super is a keyword which refers to the parent class.
{
[super init];
}
{
[super dealloc];
}

In the example above, it would begin with the superclass of the class
where reposition is defined.
Message
•  It’s the most important extension to C
•  Message is sent when one object asks another to perform a specific action.
•  Equivalent to procedural call in C
•  Simple message call looks like [receiver action], here we are asking the

.

receiver to perform the action

•  Receiver can be a object or any expression that evaluates to an object.
•  Action is the name of the method + any arguments passed to it.
Message with Arguments
•  Sometimes we pass one or more arguments along with the action to the
receiver.
•  We add a argument by adding a colon and the argument after the action like
[receiver action: argument]
•  Real world example of this is [label setText:@”This is my button”];
•  String in Objective C is defined as @””;
•  Multiple arguments can be passed to a action like this
[receiver withAction1:argument1 withacction2:argument2];
For example:
[button setTitle:@”OK” forState:NO];
Classes and Method Declaration
•  Class in objective C is a combination of two files ie .h and .m
•  .h file contains the interface of the class.
•  .m contains the implementation

Class Definition
.h file

.m file

@interface

@implementation

Variable and methods
declaration

Method definitions
Classes and Method Declaration
•  Example of a Person class.
•  Here we define the interface and implementation in Person.h and Person.m
file respectively
Person.h file
#import<Foundation/NSObject.h>

@interface Person: NSObject
{
NSString *name;
}
-(void) setName: (NSString *)str;
+(void) printCompanyName;
@end
Classes and Method Declaration
• 

Now the contents of Person.m file

• 

#import Person.h
@implementation Person
-(void) setName: (NSString *) str
{
name=str;
}
+(void) printCompanyName
{
printf(“This is class method”);
}
@end;
Here we have defined a Class Person which has a instance variable “name” and
a method “setName”.
Classes and Method Declaration
•  Using the Person class
#import<stdio.h>
#import “Person.m"
int main()
{
Person *c = [[Person alloc] init]; // Allocating and initializing Person
[c setName : @”Rahul”]; // Setting Name of the allocated person
[Person printCompanyName] // calls class method
[c release]; // releasing the person object created
return 1; // return
}
Instance and Class Methods
•  In objective C we can define methods at two levels ie Class Level and
Instance level
•  In previous Example we declared a method with a – sign prefixed. That was
a instance level method.
•  If we put + instead of – then we get a class level method.
•  A instance method can be called by the instances of the class. But a class
level can be called without creating any instance.
•  Example to call a instance method;
Person *p=[[Person alloc] init];
[p setName:@”Sunil”];
•  Example to call class method
[Person printCompanyName];
Creating multi parameter method
Objective-C enables programmer to use method with multiple
parameter. These parameter can be of same type or of different
type.
MyClass.h

MyClass.m

#import<Foundation/NSObject.h>

#import<stdio.h>
#import"MyClass.h"

@interface MyClass:NSObject{
}
// declare method for more than one
parameter
-(int) sum: (int) a andb: (int) b andc:
(int)c;
@end

@implementation MyClass
-(int) sum: (int) a andb: (int) b andc:
(int)c;{
return a+b+c;
}
@end
Creating multi parameter method
Objective-C enables programmer to use method with multiple
parameter. These parameter can be of same type or of different
type.
main.m
#import"MyClass.m"
int main()
{
MyClass *class = [[MyClass alloc]init];
NSLog(@"Sum is : %d",[class sum : 5 andb : 6 andc:10]);
[class release];
return ;
}
Output:
Sum is: 21
Constructors
•  When a class is instantiated a constructor is called which is used to initialize
the object properties
•  When a constructor is called it returns an object of a class.
•  If a user does not provide a constructor for a class the default one is used.
•  The default constructor is
-(id) init;
id is a special keyword in Objective C which can be used to refer to any
object.
•  Remember in our Person class example while instantiating the Person class
we called the constructor.
[[Person alloc] init];
It returns a person object.
Categories
•  Typically when a programmer wants to extend the functionality of a
class, he subclasses it and adds methods to it.
•  Categories can be used to add method to a class without
subclassing.
•  Here’s how you create a category
@interface PersonCategory (personcat)
@implementation PersonCategory (personcat)
Categories
• 
• 
• 

Implementation of category.
personcat.h file contains
#import “Person.h"
@interface Person (personcat)
-(void) updateName: (NSString *) str;
@end
personcat.m file contains
#import “personcat.h ”

@implementation Person (personcat)
-(void) updateName: (int)value{
Printf(“%d”,value);
}
@end
The updateName name method now behaves as if it’s the part of Person Class.
Protocols
•  Protocols are like interfaces in Java
•  It declares a set of methods, listing their arguments and return types
•  Now a class can state that its using a protocol in @interface statements in .h
file
•  For example
@interface Person:NSObject <human>
Here human is a protocol.
•  Defining a protocol
@protocol human <NSObject>
-(void) eat;
@end
Keywords Cont…
•  Memory management keywords
Keyword

Description

Alloc

Allocates memory for an object

Retain

Retains a object

Releae

Releases memory of an object

Auto release

Auto release memory used by an object
Memory Management
•  In objective C a programmer has to manage memory ie allocate and
deallocate memory for objects.
•  While instantiating a person object we allocated the memory for the object
by this call.
Person *p=[[Person alloc] init];
•  We have to release whatever objects we create programatically. Memory
management for other objects is taken care of by the Objective C runtime.
•  We use release action to release the unused memory.
The syntax for this is [p release];
Thanks

Introduction to objective c

  • 1.
  • 2.
    Objective C •  ObjectiveC is a programming language, which is used by Apple for developing the application for iPhone and Mac Systems. •  Objective C is very old programming language and it was designed and developed in 1986. Now Objective C has become popular once again as it is being used by Apple to develop applications for Mac system and iPhone. •  Full superset of C language •  Allows any traditional C code. •  Adds powerful Object oriented capabilities.
  • 3.
    OverView •  Objective Cconsists of objects, classes, instance variables, methods. •  Built entirely around objects. •  Objects like Windows, views, buttons, controllers exchange information with each other, respond to events, pass actions to run a program. •  In C we write .c and .h files, here we write .h and .m files. •  .h are the header files and .m are the source code or implementation files.
  • 4.
    Objective C Language •  •  •  •  •  Keywords Message Classesand method declaration Instance Methods and Class Methods Constructors •  User Defined Constructors •  Categories •  Protocols
  • 5.
    Keywords Keywords in objectiveC has a prefix @ appended to them. We will look at the keywords used for different purposes in this section Keyword Definition @interface This is used to declare a class/interface @implementation This is used to define class/category @protocol This is used to declare a protocol
  • 6.
    Keywords Cont… Interface The declarationof a class interface begins with the compiler directive @interface and ends with the directive @end. @interface ClassName : ItsSuperclass { instance variable declarations } method declarations @end the name of the interface file usually has the .h extension typical of header files.
  • 7.
    Keywords Cont… Implementation The definitionof a class is structured very much like its declaration. It begins with the @implementation directive and ends with the @end directive @implementation ClassName : ItsSuperclass method definitions @end The name of the implementation file has the .m extension, indicating that it contains Objective-C source code.
  • 8.
    Keywords cont.. Next arethe access modifiers. They decide the visibility/ scope of the instance variables/methods Keyword Definition @private The instance variable is accessible only within the class that declares it. @public The instance variable is accessible everywhere @protected The instance variable is accessible within the class that declares it and within classes that inherit it.
  • 9.
    Keywords Cont… Other keywords: Keyword Description @class Declaresthe names of classes defined elsewhere. @”string” Defines a constant NSString object in the current module and initializes the object with the specified string. @property Provides additional information about how the accessor methods are implemented @synthesize Tells the compiler to create the access or method(s)
  • 10.
    Keywords Cont… Declaring asimple property @interface MyClass : NSObject { float value; } @property float value; @end A property declaration is equivalent to declaring two accessor methods i.e. -(float)value; -(void)setValue:(float)newValue; These methods are not shown in the code but can be overridden.
  • 11.
    Keywords Cont… Synthesizing aproperty with @synthesize @implementation MyClass : NSObject @synthesize value; @end When a property is synthesized two accessor methods are generated i.e. -(float)value; -(void)setValue:(float)newValue; These methods are not shown in the code but they can be overridden.
  • 12.
    Keywords Cont… Self l  Self isa keyword which refers to current class. { [self setOrigin:someX :someY]; } In the example above, it would begin with the class of the object receiving the reposition message.
  • 13.
    Keywords Cont… super l  l  It beginsin the superclass of the class that defines the method where super appears. Super is a keyword which refers to the parent class. { [super init]; } { [super dealloc]; } In the example above, it would begin with the superclass of the class where reposition is defined.
  • 14.
    Message •  It’s themost important extension to C •  Message is sent when one object asks another to perform a specific action. •  Equivalent to procedural call in C •  Simple message call looks like [receiver action], here we are asking the . receiver to perform the action •  Receiver can be a object or any expression that evaluates to an object. •  Action is the name of the method + any arguments passed to it.
  • 15.
    Message with Arguments • Sometimes we pass one or more arguments along with the action to the receiver. •  We add a argument by adding a colon and the argument after the action like [receiver action: argument] •  Real world example of this is [label setText:@”This is my button”]; •  String in Objective C is defined as @””; •  Multiple arguments can be passed to a action like this [receiver withAction1:argument1 withacction2:argument2]; For example: [button setTitle:@”OK” forState:NO];
  • 16.
    Classes and MethodDeclaration •  Class in objective C is a combination of two files ie .h and .m •  .h file contains the interface of the class. •  .m contains the implementation Class Definition .h file .m file @interface @implementation Variable and methods declaration Method definitions
  • 17.
    Classes and MethodDeclaration •  Example of a Person class. •  Here we define the interface and implementation in Person.h and Person.m file respectively Person.h file #import<Foundation/NSObject.h> @interface Person: NSObject { NSString *name; } -(void) setName: (NSString *)str; +(void) printCompanyName; @end
  • 18.
    Classes and MethodDeclaration •  Now the contents of Person.m file •  #import Person.h @implementation Person -(void) setName: (NSString *) str { name=str; } +(void) printCompanyName { printf(“This is class method”); } @end; Here we have defined a Class Person which has a instance variable “name” and a method “setName”.
  • 19.
    Classes and MethodDeclaration •  Using the Person class #import<stdio.h> #import “Person.m" int main() { Person *c = [[Person alloc] init]; // Allocating and initializing Person [c setName : @”Rahul”]; // Setting Name of the allocated person [Person printCompanyName] // calls class method [c release]; // releasing the person object created return 1; // return }
  • 20.
    Instance and ClassMethods •  In objective C we can define methods at two levels ie Class Level and Instance level •  In previous Example we declared a method with a – sign prefixed. That was a instance level method. •  If we put + instead of – then we get a class level method. •  A instance method can be called by the instances of the class. But a class level can be called without creating any instance. •  Example to call a instance method; Person *p=[[Person alloc] init]; [p setName:@”Sunil”]; •  Example to call class method [Person printCompanyName];
  • 21.
    Creating multi parametermethod Objective-C enables programmer to use method with multiple parameter. These parameter can be of same type or of different type. MyClass.h MyClass.m #import<Foundation/NSObject.h> #import<stdio.h> #import"MyClass.h" @interface MyClass:NSObject{ } // declare method for more than one parameter -(int) sum: (int) a andb: (int) b andc: (int)c; @end @implementation MyClass -(int) sum: (int) a andb: (int) b andc: (int)c;{ return a+b+c; } @end
  • 22.
    Creating multi parametermethod Objective-C enables programmer to use method with multiple parameter. These parameter can be of same type or of different type. main.m #import"MyClass.m" int main() { MyClass *class = [[MyClass alloc]init]; NSLog(@"Sum is : %d",[class sum : 5 andb : 6 andc:10]); [class release]; return ; } Output: Sum is: 21
  • 23.
    Constructors •  When aclass is instantiated a constructor is called which is used to initialize the object properties •  When a constructor is called it returns an object of a class. •  If a user does not provide a constructor for a class the default one is used. •  The default constructor is -(id) init; id is a special keyword in Objective C which can be used to refer to any object. •  Remember in our Person class example while instantiating the Person class we called the constructor. [[Person alloc] init]; It returns a person object.
  • 24.
    Categories •  Typically whena programmer wants to extend the functionality of a class, he subclasses it and adds methods to it. •  Categories can be used to add method to a class without subclassing. •  Here’s how you create a category @interface PersonCategory (personcat) @implementation PersonCategory (personcat)
  • 25.
    Categories •  •  •  Implementation of category. personcat.hfile contains #import “Person.h" @interface Person (personcat) -(void) updateName: (NSString *) str; @end personcat.m file contains #import “personcat.h ” @implementation Person (personcat) -(void) updateName: (int)value{ Printf(“%d”,value); } @end The updateName name method now behaves as if it’s the part of Person Class.
  • 26.
    Protocols •  Protocols arelike interfaces in Java •  It declares a set of methods, listing their arguments and return types •  Now a class can state that its using a protocol in @interface statements in .h file •  For example @interface Person:NSObject <human> Here human is a protocol. •  Defining a protocol @protocol human <NSObject> -(void) eat; @end
  • 27.
    Keywords Cont… •  Memorymanagement keywords Keyword Description Alloc Allocates memory for an object Retain Retains a object Releae Releases memory of an object Auto release Auto release memory used by an object
  • 28.
    Memory Management •  Inobjective C a programmer has to manage memory ie allocate and deallocate memory for objects. •  While instantiating a person object we allocated the memory for the object by this call. Person *p=[[Person alloc] init]; •  We have to release whatever objects we create programatically. Memory management for other objects is taken care of by the Objective C runtime. •  We use release action to release the unused memory. The syntax for this is [p release];
  • 29.