SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Common language runtime clr
Common language runtime clrCommon language runtime clr
Common language runtime clr
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Client Server Computing : unit 1
Client Server Computing : unit 1Client Server Computing : unit 1
Client Server Computing : unit 1
 
Java Notes
Java Notes Java Notes
Java Notes
 
interface in c#
interface in c#interface in c#
interface in c#
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
C# basics
 C# basics C# basics
C# basics
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Menu bars and menus
Menu bars and menusMenu bars and menus
Menu bars and menus
 
Importance & Principles of Modeling from UML Designing
Importance & Principles of Modeling from UML DesigningImportance & Principles of Modeling from UML Designing
Importance & Principles of Modeling from UML Designing
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 

Andere mochten auch

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 Eakapong Kattiya
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application DevelopmentDhaval Kaneria
 
Introduction to xcode
Introduction to xcodeIntroduction to xcode
Introduction to xcodeSunny Shaikh
 
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 APIsSubhransu Behera
 
Objective-C A Beginner's Dive
Objective-C A Beginner's DiveObjective-C A Beginner's Dive
Objective-C A Beginner's DiveAltece
 
Apple iOS Introduction
Apple iOS IntroductionApple iOS Introduction
Apple iOS IntroductionPratik Vyas
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CDataArt
 

Andere mochten auch (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
 

Ähnlich wie Introduction to objective c

Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4thConnex
 
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 01Zafor Iqbal
 
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 PHPVibrant Technologies & Computers
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsakreyi
 
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 Manykenatmxm
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptxarjun431527
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java DevelopersMuhammad Abdullah
 
[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 TypesMuhammad Hammad Waseem
 
Java-Intro.pptx
Java-Intro.pptxJava-Intro.pptx
Java-Intro.pptxVijalJain3
 

Ähnlich wie 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
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Objective c
Objective cObjective c
Objective c
 
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
 
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
 

Kürzlich hochgeladen

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
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)wesley chun
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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 2024The Digital Insurer
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
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 BusinessPixlogix Infotech
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
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 Scriptwesley chun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
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.pdfsudhanshuwaghmare1
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

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