SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 

Tendances (20)

Android Toast.pdf
Android Toast.pdfAndroid Toast.pdf
Android Toast.pdf
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
 
Java Streams
Java StreamsJava Streams
Java Streams
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java tokens
Java tokensJava tokens
Java tokens
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Validation Controls in asp.net
Validation Controls in asp.netValidation Controls in asp.net
Validation Controls in asp.net
 
Kotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptxKotlin Basics & Introduction to Jetpack Compose.pptx
Kotlin Basics & Introduction to Jetpack Compose.pptx
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
 
Android resources
Android resourcesAndroid resources
Android resources
 
Features of java
Features of javaFeatures of java
Features of java
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Java Collections
Java  Collections Java  Collections
Java Collections
 

En vedette

Introduction to xcode
Introduction to xcodeIntroduction to xcode
Introduction to xcode
Sunny Shaikh
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
DataArt
 

En vedette (12)

iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
iOS Basic Development Day 2 - Objective-C 2.0 & iOS Framework
 
iOS Basic
iOS BasiciOS Basic
iOS Basic
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Introduction to xcode
Introduction to xcodeIntroduction to xcode
Introduction to xcode
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIs
 
Advanced iOS
Advanced iOSAdvanced iOS
Advanced iOS
 
Objective-C A Beginner's Dive
Objective-C A Beginner's DiveObjective-C A Beginner's Dive
Objective-C A Beginner's Dive
 
Apple iOS Introduction
Apple iOS IntroductionApple iOS Introduction
Apple iOS Introduction
 
Ios operating system
Ios operating systemIos operating system
Ios operating system
 
Presentation on iOS
Presentation on iOSPresentation on iOS
Presentation on iOS
 
Apple iOS
Apple iOSApple iOS
Apple iOS
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
 

Similaire à Introduction to objective c

Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
Connex
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
Zafor Iqbal
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
arjun431527
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
Muhammad Abdullah
 

Similaire à Introduction to objective c (20)

Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01C Sharp: Basic to Intermediate Part 01
C Sharp: Basic to Intermediate Part 01
 
PHP - Introduction to Object Oriented Programming with PHP
PHP -  Introduction to  Object Oriented Programming with PHPPHP -  Introduction to  Object Oriented Programming with PHP
PHP - Introduction to Object Oriented Programming with PHP
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Intro to iOS Development • Made by Many
Intro to iOS Development • Made by ManyIntro to iOS Development • Made by Many
Intro to iOS Development • Made by Many
 
C#2
C#2C#2
C#2
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
My c++
My c++My c++
My c++
 
iOS Application Development
iOS Application DevelopmentiOS Application Development
iOS Application Development
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Unit 5.ppt
Unit 5.pptUnit 5.ppt
Unit 5.ppt
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptx
 
201005 accelerometer and core Location
201005 accelerometer and core Location201005 accelerometer and core Location
201005 accelerometer and core Location
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 

Dernier

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

Introduction to objective c

  • 2. 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.
  • 3. 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.
  • 4. Objective C Language •  •  •  •  •  Keywords Message Classes and method declaration Instance Methods and Class Methods Constructors •  User Defined Constructors •  Categories •  Protocols
  • 5. 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
  • 6. 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.
  • 7. 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.
  • 8. 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.
  • 9. 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)
  • 10. 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.
  • 11. 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.
  • 12. 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.
  • 13. 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.
  • 14. 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.
  • 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 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
  • 17. 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
  • 18. 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”.
  • 19. 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 }
  • 20. 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];
  • 21. 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
  • 22. 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
  • 23. 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.
  • 24. 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)
  • 25. 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.
  • 26. 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
  • 27. 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
  • 28. 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];