SlideShare a Scribd company logo
iOS Application Development

Objective C

Ashiq Uz Zoha (Ayon),!
ashiq.ayon@gmail.com,!
Dhrubok Infotech Services Ltd.
Introduction
❖

Objective-C is a general-purpose, object-oriented
programming language that adds Smalltalk-style
messaging to the C programming language.!

❖

It is the main programming language used by Apple for
the OS X and iOS operating systems and their respective
APIs, Cocoa and Cocoa Touch.!

❖

Originally developed in the early 1980s, it was selected
as the main language used by NeXT for its NeXTSTEP
operating system, from which OS X and iOS are derived.
Introduction

❖

Objective-C was created primarily by Brad Cox and
Tom Love in the early 1980s at their company Stepstone.!

❖

Uses power of C and features of SmallTalk. We’ll see
later.
Syntax

❖

Objective C syntax is completely different that we have
used so far in the programming languages we know.
Let’s have a look…
Message
❖

Objective C objects sends messages to another object.
It’s similar to calling a method in our known language
like C++ and Java .!
obj->method(argument); // C++!
obj.method(argument); // java
[obj method:argument] ; // Objective C

Object / Instance

Name of Method

Parameters
Interfaces and implementations
❖

Objective-C requires that the interface and implementation of a class be in
separately declared code blocks. !

❖

By convention, developers place the interface in a header file and the
implementation in a code file. The header files, normally suffixed .h, are similar
to C header files while the implementation (method) files, normally
suffixed .m, can be very similar to C code files.

Objective C Class
•
•

Header / Interface file!
.h extension

•
•

Implementation file!
.m extension!
Interface
❖

NOT “User Interface” or “Interface of OOP in Java” :)!

❖

In other programming languages, this is called a "class
definition”.!

❖

The interface of a class is usually defined in a header
file.
Interface
Implementation
Object Creation

Pointer
:-D

Allocate

MyClass *objectName = [[MyClass alloc] init] ;
Initialize
Methods
❖

Method is declared in objective C as follows!

•

Declaration : !
- (returnType) methodName:(typeName) variable1 :
(typeName)variable2;!

•

Example : !
-(void) calculateAreaForRectangleWithLength:(CGfloat) length ;!

•

Calling the method: !
[self calculateAreaForRectangleWithLength:30];
Class Method
❖

Class methods can be accessed directly without creating
objects for the class. They don't have any variables and
objects associated with it.!

❖

Example :!

•

+(void)simpleClassMethod;!

❖

It can be accessed by using the class name (let's assume
the class name as MyClass) as follows.!

•

[MyClass simpleClassMethod];
Class Method
Sounds Familiar ?!
❖

Can we remember something like “Static
Method” ? !

•

public static void MethodName(){}!

•

Classname.MethodName() ; // static method!

•

[ClassName MethodName]; // class method
Instance methods
❖

Instance methods can be accessed only after creating an
object for the class. Memory is allocated to the instance
variables.!

❖

Example :!

•

-(void)simpleInstanceMethod; !

❖

Accessing the method :!
MyClass *objectName = [[MyClass alloc]init] ;!
[objectName simpleInstanceMethod];
Property
❖

For an external class to access class variables properties
are used.!

❖

Example: @property(nonatomic , strong) NSString
*myString;!

❖

You can use dot operator to access properties. To access
the above property we will do the following.!

❖

self.myString = @“Test"; or [self setMyString:@"Test"];
Memory Management
❖

Memory management is the programming discipline of
managing the life cycles of objects and freeing them
when they are no longer needed.!

❖

Memory management in a Cocoa application that
doesn’t use garbage collection is based on a reference
counting model.!

❖

When you create or copy an object, its retain count is 1.
Thereafter other objects may express an ownership
interest in your object, which increments its retain count.
Memory Management Rules
•

You own any object you create by allocating memory for it or copying it. !

!

mutableCopy,

Related methods: alloc, allocWithZone:, copy, copyWithZone:, !
mutableCopyWithZone:!
•

!
•

!
•

If you are not the creator of an object, but want to ensure it stays in memory for you
to use, you can express an ownership interest in it.!
Related method: retain!
If you own an object, either by creating it or expressing an ownership interest, you are
responsible for releasing it when you no longer need it.!
Related methods: release, autorelease!
Conversely, if you are not the creator of an object and have not expressed an
ownership interest, you must not release it.
Reference Counting System
❖

object-ownership scheme is implemented through a
reference-counting system that internally tracks how
many owners each object has. When you claim
ownership of an object, you increase it’s reference count,
and when you’re done with the object, you decrease its
reference count.!

❖

While its reference count is greater than zero, an object
is guaranteed to exist, but as soon as the count reaches
zero, the operating system is allowed to destroy it.
•

In the past, developers manually controlled an object’s
reference count by calling special memory-management
methods defined by the NSObject protocol. This is called
Manual Retain Release (MRR). !
!

•

In a Manual Retain Release environment, it’s your job to claim
and relinquish ownership of every object in your program. You
do this by calling special memory-related methods,
MRR
alloc

Create an object and
claim ownership of it.

retain

Claim ownership of an
existing object.

copy

Copy an object and
claim ownership of it.

release

Relinquish ownership of
an object and destroy it
immediately.

autorelease

Relinquish ownership of
an object but defer its
destruction.
We don’t need them Now :)
Automatic Reference Counting
❖

Automatic Reference Counting works the exact same
way as MRR, but it automatically inserts the
appropriate memory-management methods for you.!

❖

This is a big deal for Objective-C developers, as it lets
them focus entirely on what their application needs to
do rather than how it does it.
Constructors , Destructors
❖

Constructor in objective C is technically just “init” method.!

❖

Default constructor for every object is !

-(id) init { !
!

!

!

!

self = [super init] ; !

!

!

!

!

return self ; !

!

!

!

}!

❖

Like Java , Objective C has only parent class , i,e NSObject. So, we
access constructor of super class by [super init];
Protocols
❖

A protocol is a group of related properties and methods that can
be implemented by any class.!

❖

They are more flexible than a normal class interface, since they let
you reuse a single API declaration in completely unrelated classes.!
Protocol Definition
❖

Here is an example of a protocol which includes one method, notice
the instance variable delegate is of type id, as it will be unknown at
compile time the type of class that will adopt this protocol.
Adopting the Protocol
❖

To keep the example short, I am using the application
delegate as the class that adopts the protocol. Here is how
the app delegate looks:
Blocks
❖

An Objective-C class defines an object that combines data with
related behaviour. Sometimes, it makes sense just to represent a
single task or unit of behaviour, rather than a collection of
methods.!

❖

Blocks are a language-level feature added to C, Objective-C and
C++, which allow you to create distinct segments of code that
can be passed around to methods or functions as if they were
values.!

❖

Blocks are Objective-C objects, which means they can be added
to collections like NSArray or NSDictionary.
Blocks
Exceptions & Errors
❖

Exceptions can be handled using the standard try-catchfinally pattern found in most other high-level
programming languages. First, you need to place any
code that might result in an exception in an @try block.
Then, if an exception is thrown, the corresponding
@catch() block is executed to handle the problem. The
@finally block is called afterwards, regardless of
whether or not an exception occurred.
Exceptions & Errors
“Thank You”

More Related Content

PPTX
Classes And Objects
PPTX
PPT
Java core - Detailed Overview
PPT
Object-oriented concepts
PPTX
Introduction to oop and java fundamentals
PPTX
Typescript
PPTX
Java object oriented programming concepts - Brainsmartlabs
PDF
Beginning OOP in PHP
Classes And Objects
Java core - Detailed Overview
Object-oriented concepts
Introduction to oop and java fundamentals
Typescript
Java object oriented programming concepts - Brainsmartlabs
Beginning OOP in PHP

What's hot (20)

KEY
Java Building Blocks
PPTX
01 Java Language And OOP PART I
PPSX
Core java lessons
PPTX
Intro to OOP PHP and Github
PPTX
Introduction to java
PPTX
PPT
INTRODUCTION TO JAVA
PDF
OOPs Concepts - Android Programming
PPTX
Basic online java course - Brainsmartlabs
PPTX
Core Java
PPT
core java course online
PPTX
Chapter 2 java
PPTX
Is2215 lecture2 student(2)
PPTX
Beginning Java for .NET developers
PPTX
Java principles
PPT
Java Basics
PPTX
04 Java Language And OOP Part IV
PPT
Core java Basics
PDF
Metaprogramming ruby
PPT
OOP programming
Java Building Blocks
01 Java Language And OOP PART I
Core java lessons
Intro to OOP PHP and Github
Introduction to java
INTRODUCTION TO JAVA
OOPs Concepts - Android Programming
Basic online java course - Brainsmartlabs
Core Java
core java course online
Chapter 2 java
Is2215 lecture2 student(2)
Beginning Java for .NET developers
Java principles
Java Basics
04 Java Language And OOP Part IV
Core java Basics
Metaprogramming ruby
OOP programming
Ad

Viewers also liked (20)

PPTX
Introduction to objective c
PDF
Introduction to objective c
PDF
iPhone Programming [1/17] : Objective-C
KEY
Objective-C: a gentle introduction
PPTX
Ndu06 typesof language
PDF
Introduction to objective c
PPT
I Phone Development Presentation
PDF
Objective-C for Beginners
PPTX
Introduction to Objective - C
PDF
Iphone programming: Objective c
PPTX
Hybrid vs Native Mobile App. Decide in 5 minutes!
PPT
Objective-C for iOS Application Development
PDF
Introduction to Objective - C
PPTX
High Level Languages (Imperative, Object Orientated, Declarative)
PPTX
Object-Orientated Design
PDF
React Native Introduction: Making Real iOS and Android Mobile App By JavaScript
PPTX
Appraisal (Self Assessment, Peer Assessment, 360 Degree Feedback)
PPT
Object Oriented Design
PDF
Objective-C
PPT
Object Oriented Analysis and Design
Introduction to objective c
Introduction to objective c
iPhone Programming [1/17] : Objective-C
Objective-C: a gentle introduction
Ndu06 typesof language
Introduction to objective c
I Phone Development Presentation
Objective-C for Beginners
Introduction to Objective - C
Iphone programming: Objective c
Hybrid vs Native Mobile App. Decide in 5 minutes!
Objective-C for iOS Application Development
Introduction to Objective - C
High Level Languages (Imperative, Object Orientated, Declarative)
Object-Orientated Design
React Native Introduction: Making Real iOS and Android Mobile App By JavaScript
Appraisal (Self Assessment, Peer Assessment, 360 Degree Feedback)
Object Oriented Design
Objective-C
Object Oriented Analysis and Design
Ad

Similar to Intro to Objective C (20)

PPT
Introducing object oriented programming (oop)
PPTX
Introduction to PHP and MySql basics.pptx
PDF
Bootstrapping iPhone Development
PPTX
Class and Objects in python programming.pptx
PPTX
Presentation 1st
PPT
Java Fundamentalojhgghjjjjhhgghhjjjjhhj.ppt
PPT
UNIT-IV WT web technology for 1st year cs
PPTX
Object Oriented Programming - Copy.pptxb
PPTX
Summer Training Project On C++
PPTX
c++.pptxwjwjsijsnsksomammaoansnksooskskk
PPTX
Programming Language
PPTX
C++ Introduction brown bag
PPTX
oop.pptx
PPTX
CPP13 - Object Orientation
PPTX
object oriented programming unit one ppt
PPTX
Presentation 3rd
PPTX
Object-oriented programming 3.pptx
PPTX
Objective-c for Java Developers
PPTX
Object oriented programming
PPTX
SE-IT JAVA LAB OOP CONCEPT
Introducing object oriented programming (oop)
Introduction to PHP and MySql basics.pptx
Bootstrapping iPhone Development
Class and Objects in python programming.pptx
Presentation 1st
Java Fundamentalojhgghjjjjhhgghhjjjjhhj.ppt
UNIT-IV WT web technology for 1st year cs
Object Oriented Programming - Copy.pptxb
Summer Training Project On C++
c++.pptxwjwjsijsnsksomammaoansnksooskskk
Programming Language
C++ Introduction brown bag
oop.pptx
CPP13 - Object Orientation
object oriented programming unit one ppt
Presentation 3rd
Object-oriented programming 3.pptx
Objective-c for Java Developers
Object oriented programming
SE-IT JAVA LAB OOP CONCEPT

Recently uploaded (20)

PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
01-Introduction-to-Information-Management.pdf
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Open Quiz Monsoon Mind Game Final Set.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
GDM (1) (1).pptx small presentation for students
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
01-Introduction-to-Information-Management.pdf
The Final Stretch: How to Release a Game and Not Die in the Process.
Microbial diseases, their pathogenesis and prophylaxis
Renaissance Architecture: A Journey from Faith to Humanism
O5-L3 Freight Transport Ops (International) V1.pdf
Insiders guide to clinical Medicine.pdf
Pharma ospi slides which help in ospi learning
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
2.FourierTransform-ShortQuestionswithAnswers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

Intro to Objective C

  • 1. iOS Application Development Objective C Ashiq Uz Zoha (Ayon),! [email protected],! Dhrubok Infotech Services Ltd.
  • 2. Introduction ❖ Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language.! ❖ It is the main programming language used by Apple for the OS X and iOS operating systems and their respective APIs, Cocoa and Cocoa Touch.! ❖ Originally developed in the early 1980s, it was selected as the main language used by NeXT for its NeXTSTEP operating system, from which OS X and iOS are derived.
  • 3. Introduction ❖ Objective-C was created primarily by Brad Cox and Tom Love in the early 1980s at their company Stepstone.! ❖ Uses power of C and features of SmallTalk. We’ll see later.
  • 4. Syntax ❖ Objective C syntax is completely different that we have used so far in the programming languages we know. Let’s have a look…
  • 5. Message ❖ Objective C objects sends messages to another object. It’s similar to calling a method in our known language like C++ and Java .! obj->method(argument); // C++! obj.method(argument); // java [obj method:argument] ; // Objective C Object / Instance Name of Method Parameters
  • 6. Interfaces and implementations ❖ Objective-C requires that the interface and implementation of a class be in separately declared code blocks. ! ❖ By convention, developers place the interface in a header file and the implementation in a code file. The header files, normally suffixed .h, are similar to C header files while the implementation (method) files, normally suffixed .m, can be very similar to C code files. Objective C Class • • Header / Interface file! .h extension • • Implementation file! .m extension!
  • 7. Interface ❖ NOT “User Interface” or “Interface of OOP in Java” :)! ❖ In other programming languages, this is called a "class definition”.! ❖ The interface of a class is usually defined in a header file.
  • 10. Object Creation Pointer :-D Allocate MyClass *objectName = [[MyClass alloc] init] ; Initialize
  • 11. Methods ❖ Method is declared in objective C as follows! • Declaration : ! - (returnType) methodName:(typeName) variable1 : (typeName)variable2;! • Example : ! -(void) calculateAreaForRectangleWithLength:(CGfloat) length ;! • Calling the method: ! [self calculateAreaForRectangleWithLength:30];
  • 12. Class Method ❖ Class methods can be accessed directly without creating objects for the class. They don't have any variables and objects associated with it.! ❖ Example :! • +(void)simpleClassMethod;! ❖ It can be accessed by using the class name (let's assume the class name as MyClass) as follows.! • [MyClass simpleClassMethod];
  • 13. Class Method Sounds Familiar ?! ❖ Can we remember something like “Static Method” ? ! • public static void MethodName(){}! • Classname.MethodName() ; // static method! • [ClassName MethodName]; // class method
  • 14. Instance methods ❖ Instance methods can be accessed only after creating an object for the class. Memory is allocated to the instance variables.! ❖ Example :! • -(void)simpleInstanceMethod; ! ❖ Accessing the method :! MyClass *objectName = [[MyClass alloc]init] ;! [objectName simpleInstanceMethod];
  • 15. Property ❖ For an external class to access class variables properties are used.! ❖ Example: @property(nonatomic , strong) NSString *myString;! ❖ You can use dot operator to access properties. To access the above property we will do the following.! ❖ self.myString = @“Test"; or [self setMyString:@"Test"];
  • 16. Memory Management ❖ Memory management is the programming discipline of managing the life cycles of objects and freeing them when they are no longer needed.! ❖ Memory management in a Cocoa application that doesn’t use garbage collection is based on a reference counting model.! ❖ When you create or copy an object, its retain count is 1. Thereafter other objects may express an ownership interest in your object, which increments its retain count.
  • 17. Memory Management Rules • You own any object you create by allocating memory for it or copying it. ! ! mutableCopy, Related methods: alloc, allocWithZone:, copy, copyWithZone:, ! mutableCopyWithZone:! • ! • ! • If you are not the creator of an object, but want to ensure it stays in memory for you to use, you can express an ownership interest in it.! Related method: retain! If you own an object, either by creating it or expressing an ownership interest, you are responsible for releasing it when you no longer need it.! Related methods: release, autorelease! Conversely, if you are not the creator of an object and have not expressed an ownership interest, you must not release it.
  • 18. Reference Counting System ❖ object-ownership scheme is implemented through a reference-counting system that internally tracks how many owners each object has. When you claim ownership of an object, you increase it’s reference count, and when you’re done with the object, you decrease its reference count.! ❖ While its reference count is greater than zero, an object is guaranteed to exist, but as soon as the count reaches zero, the operating system is allowed to destroy it.
  • 19. • In the past, developers manually controlled an object’s reference count by calling special memory-management methods defined by the NSObject protocol. This is called Manual Retain Release (MRR). ! ! • In a Manual Retain Release environment, it’s your job to claim and relinquish ownership of every object in your program. You do this by calling special memory-related methods,
  • 20. MRR alloc Create an object and claim ownership of it. retain Claim ownership of an existing object. copy Copy an object and claim ownership of it. release Relinquish ownership of an object and destroy it immediately. autorelease Relinquish ownership of an object but defer its destruction.
  • 21. We don’t need them Now :)
  • 22. Automatic Reference Counting ❖ Automatic Reference Counting works the exact same way as MRR, but it automatically inserts the appropriate memory-management methods for you.! ❖ This is a big deal for Objective-C developers, as it lets them focus entirely on what their application needs to do rather than how it does it.
  • 23. Constructors , Destructors ❖ Constructor in objective C is technically just “init” method.! ❖ Default constructor for every object is ! -(id) init { ! ! ! ! ! self = [super init] ; ! ! ! ! ! return self ; ! ! ! ! }! ❖ Like Java , Objective C has only parent class , i,e NSObject. So, we access constructor of super class by [super init];
  • 24. Protocols ❖ A protocol is a group of related properties and methods that can be implemented by any class.! ❖ They are more flexible than a normal class interface, since they let you reuse a single API declaration in completely unrelated classes.!
  • 25. Protocol Definition ❖ Here is an example of a protocol which includes one method, notice the instance variable delegate is of type id, as it will be unknown at compile time the type of class that will adopt this protocol.
  • 26. Adopting the Protocol ❖ To keep the example short, I am using the application delegate as the class that adopts the protocol. Here is how the app delegate looks:
  • 27. Blocks ❖ An Objective-C class defines an object that combines data with related behaviour. Sometimes, it makes sense just to represent a single task or unit of behaviour, rather than a collection of methods.! ❖ Blocks are a language-level feature added to C, Objective-C and C++, which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values.! ❖ Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDictionary.
  • 29. Exceptions & Errors ❖ Exceptions can be handled using the standard try-catchfinally pattern found in most other high-level programming languages. First, you need to place any code that might result in an exception in an @try block. Then, if an exception is thrown, the corresponding @catch() block is executed to handle the problem. The @finally block is called afterwards, regardless of whether or not an exception occurred.