SlideShare a Scribd company logo
TECNOLOGIAS EMERGENTES EM JOGOS

Engenharia em Desenvolvimento de Jogos Digitais - 2013/2014

February 18, 2014

Daniela da Cruz
AGENDA
1. The iOS architecture

2
AGENDA
1. The iOS architecture
2. Objective-C
2.1

History

2.2

Objective-C primitives

2.3

Foundation Framework  Data Types (Part I)

2.4

Pointers

2.5

Classes

2.6

Namespaces

2.7

Utilities

2
THE IOS ARCHITECTURE
THE IOS ARCHITECTURE
→

iOS is the operating system that runs on iPad, iPhone, and
iPod touch devices.

→

At the highest level, iOS acts as an intermediary between the
underlying hardware and the apps we create.

→

Apps do not talk to the underlying hardware directly. Instead,
they communicate with the hardware through a set of
well-dened system interfaces.

→

These interfaces make it easy to write apps that work
consistently on devices having dierent hardware capabilities.

4
THE IOS ARCHITECTURE
The implementation of iOS technologies can be viewed as a set of
layers.

5
THE IOS ARCHITECTURE

→

Lower layers contain fundamental services and technologies.

→

Higher-level layers build upon the lower layers and provide
more sophisticated services and technologies.

6
THE IOS ARCHITECTURE  CORE OS

The Core OS layer contains the low-level features that most other
technologies are built upon.

7
THE IOS ARCHITECTURE  CORE OS
The layer provides a variety of services including:

→

low level networking;

→

access to external accessories;

→

and the usual fundamental operating system services such as
memory management, le system handling and threads.

8
THE IOS ARCHITECTURE  CORE SERVICES

The iOS Core Services layer provides much of the foundation on
which the other layers are built.

9
THE IOS ARCHITECTURE  CORE SERVICES

Key among these services are the Core Foundation and Foundation
frameworks, which dene the basic types that all apps use.
This layer also contains individual technologies to support features
such as location, iCloud, social media, and networking.

10
THE IOS ARCHITECTURE  MEDIA

The Media layer contains the graphics, audio, and video
technologies you use to implement multimedia experiences in our
apps.
The technologies in this layer make easier to build apps that look
and sound great.

11
THE IOS ARCHITECTURE  COCOA

The Cocoa Touch layer contains key frameworks for building iOS
apps.
These frameworks dene the appearance of your app.
They also provide the basic app infrastructure and support for key
technologies such as multitasking, touch-based input, push
notications, and many high-level system services.

12
OBJECTIVE-C
1. The iOS architecture

2. Objective-C
2.1

History

2.2

Objective-C primitives

2.3

Foundation Framework  Data Types (Part I)

2.4

Pointers

2.5

Classes

2.6

Namespaces

2.7

Utilities

14
HISTORICAL CONTEXT
→ 1972

→ 1980

→ 1983

→ 1986

 C programming language
 SmallTalk (rst OO language)
 C++ (C extension with OO support)
 Objective-C (C extension with concepts from

SmallTalk)

15
WHY OBJECTIVE-C?
→

Was chosen as the main language in NeXT company founded
by Steve Jobs to develop the operating System NeXTStep

→

In 1996, Apple buys NeXT and starting the development of
Mac OS X (2001) using Objective-C

Objective-C is the native programming language for Apple's iOS
and OS X operating systems.

16
OBJECTIVE-C
It's a compiled, general-purpose language capable of building
everything from command line utilities to animated GUIs to
domain-specic libraries.

17
OBJECTIVE-C
Objective-C is a strict superset of C, which means that it's possible
to seamlessly combine both languages in the same source le.
In fact, Objective-C relies on C for most of its core language

t's important to have at least a basic
foundation in C before tackling the higher-level aspects of the

constructs, so i
language.

18
OBJECTIVE-C
Like C++, Objective-C was designed to add object-oriented
features to C, but the two languages accomplished this using
fundamentally distinct philosophies.

→

Objective-C is more dynamic, deferring most of its decisions to
run-time rather than compile-time.

→

Objective-C is also known for its verbose naming conventions.

19
OBJECTIVE-C
C++
1

john-drive(Corvette, Mary's House)

Objective-C
1

[john driveCar:@Corvette toDestination:@Mary's House]

Objective-C method calls read more like a human language
than a computer one.

20
OBJECTIVE-C
As with plain old C programs, the

main()

function serves as the

root of an Objective-C application.

An autoreleasepool implements simple garbage collection for reference-counted objects. It temporarily
takes ownwership of reference-counted objects that nobody else wants to take ownership of and releases
them at a later, appropriate point in time.

21
1. The iOS architecture

2. Objective-C
2.1

History

2.2

Objective-C primitives

2.3

Foundation Framework  Data Types (Part I)

2.4

Pointers

2.5

Classes

2.6

Namespaces

2.7

Utilities

22
ID TYPE
The

id

type is the generic type for all Objective-C objects. You can

think of it as the object-oriented version of C's void pointer
(void*).
And, like a void pointer, it can store a reference to any type of
object.

id mysteryObject = @An NSString object;

23
CLASS TYPE
Objective-C classes are represented as objects themselves, using a
special data type called

Class.

This lets you, for example, dynamically check an object's type at
runtime (introspection  for later discussion).

Class targetClass = [NSString class];
id mysteryObject = @An NSString object;
if ([mysteryObject isKindOfClass:targetClass]) {
NSLog(@Yup! That's an instance of the target class);
}

24
SEL TYPE
The

SEL

data type is used to store selectors, which are

Objective-C's internal representation of a method name.
For example, the following snippet stores a method called

sayHello

in the

someMethod

variable. This variable could be used

to dynamically call a method at runtime.

SEL someMethod = @selector(sayHello);

25
1. The iOS architecture

2. Objective-C
2.1

History

2.2

Objective-C primitives

2.3

Foundation Framework  Data Types (Part I)

2.4

Pointers

2.5

Classes

2.6

Namespaces

2.7

Utilities

26
FOUNDATION FRAMEWORK  DATA TYPES (PART I)

NSObject

 is the root class of most Objective-C class hierarchies.

Is the base class for pretty much every object in the iOS SDK.

27
FOUNDATION FRAMEWORK  DATA TYPES
BOOL  used for representing Boolean (YES or NO are BOOL
types allowed).

BOOL myBOOLyes = YES;
BOOL myBOOLno = NO;

28
FOUNDATION FRAMEWORK  DATA TYPES
NSString  used for representing a string instead of C language's
char*
→
→

type.

An NSString instance

immutable.

can not be modied! They are

Tons of utility functions available (case conversion, URLs,
substrings, type conversions, etc.).

NSString* hi = @Hi;

29
FOUNDATION FRAMEWORK  DATA TYPES
NSMutableString  Mutable version of NSString. (Somewhat
rarely used.)

NSMutableString * str1 = [NSMutableString
stringWithString:@Hello World];

30
FOUNDATION FRAMEWORK  DATA TYPES
NSNumber  Object wrapper around primitive types like int,

float, double, BOOL,

etc.

NSNumber *num = [NSNumber numberWithInt:36];
float f = [num floatValue]; // would return 36 as a float
// (i.e. will convert types)

31
FOUNDATION FRAMEWORK  DATA TYPES
NSValue  Generic object wrapper for other non-object data
types.

// CGPoint is a C struct
CGPoint point = CGPointMake(25.0, 15.0);
NSValue *pointObject = [NSValue valueWithCGPoint:point];

32
FOUNDATION FRAMEWORK  DATA TYPES
NSDate  Used to nd out the time right now or to store past or
future times/dates.
An

NSDate

object represents a specic point in time, independent

of any particular calendrical system, time zone, or locale.
Internally, it just records the number of seconds from an arbitrary
reference point (January 1st, 2001 GMT).

NSDate *now = [NSDate date];

33
1. The iOS architecture

2. Objective-C
2.1

History

2.2

Objective-C primitives

2.3

Foundation Framework  Data Types (Part I)

2.4

Pointers

2.5

Classes

2.6

Namespaces

2.7

Utilities

34
POINTERS

All Objective-C objects are referenced as pointers.
An NSString object must be stored as a pointer, not a normal
variable:

NSString *model = @Honda;

35
POINTERS
There is a slight dierence between C and Objective-C.
Whereas C uses

NULL,

Objective-C denes its own macro,

nil,

as

its null object.

NSString *anObject;
anObject = NULL;
anObject = nil;

// An Objective-C object
// This will work
// But this is preferred

int *aPointer;
aPointer = nil;
aPointer = NULL;

// A plain old C pointer
// Don't do this
// Do this instead

36
NIL VERSUS NULL

Objective-C's

nill

keyword is similar to

NULL

in other languages

like C and Java.
The dierence is that it is perfectly safe to call methods on
If any method that returns an object is called on
receive a

nil

nill,

nil.

we will

back.

37
1. The iOS architecture

2. Objective-C
2.1

History

2.2

Objective-C primitives

2.3

Foundation Framework  Data Types (Part I)

2.4

Pointers

2.5

Classes

2.6

Namespaces

2.7

Utilities

38
CLASSES
When we dene a class, we dene a

blueprint for a data type.

This doesn't actually dene any data, but it does dene what the
class name means, that is:

→

what an object of the class will consist of; and

→

what operations can be performed on such an object.

39
CLASSES  INTERFACE
An interface declares the public properties and methods of a class.

@interface Photo : NSObject
{
// Protected instance variables (not recommended)
}
// Properties and Methods
@end
Protected variables can be dened inside of the curly braces, but
most developers treat instance variables as implementation details
and prefer to store them in the

.m

le instead of the interface.

40
CLASSES  INTERFACE
When declaring a method, ask yourself:

Does the method apply to only one instance or all instances
of the class?
If it applies to all instances, it's usually better as a class method.
Class methods are preceded by a minus (-) symbol.
Instance methods are preceded by a plus (+) symbol.
→

41
CLASSES  INTERFACE
Example:

@interface Photo : NSObject
- (NSString*) caption;
- (NSString*) photographer;
- (void) setCaption: (NSString*)input;
- (void) setPhotographer: (NSString*)input;
@end

42
CLASSES  IMPLEMENTATION
The

@implementation

directive is similar to

@interface,

except

you don't need to include the super class.

@implementation Photo {
// Private instance variables
}
// Methods
@end
The instance variables are

private and are only accessible inside the

class implementation.

43
CLASSES  IMPLEMENTATION
Continuation of previous example:

@implementation Photo
{
NSString* caption;
NSString* photographer;
}
- (NSString*) caption { return caption; }
- (NSString*) photographer {
return photographer;
}
@end
44
CLASSES  IMPLEMENTATION
The getters themselves are very simple. They just return the
instance variable.
A setter's job is to swap the old value with the new one.
Objective-C objects are dierent than an

int

or

float.

assign an object to a variable, it does not get the

reference to the object.

When you

value; it gets a

45
CLASSES  IMPLEMENTATION

- (void) setCaption: (NSString*) input {
[caption autorelease];
[input retain];
caption = input;
}

46
CLASSES  IMPLEMENTATION
1.

[caption autorelease]
We no longer need the old NSString object that caption refers
so, so we call autorelease on it.

2.

[input retain]
We call retain on the input object because we are going to
assign it to the instance variable and need it to stay around.

3.

caption = input
Finally we set the caption instance variable to be equal to
input.

47
DO I ALWAYS NEED TO DO THIS?

NO

but only if you choose

Automatic Reference Counting (ARC) to be
used in your project!!!

48
CLASSES  IMPLEMENTATION
Classes usually have an

init

method to set initial values for its

instance variables or other setup tasks.

- (id) init {
[self setCaption:@Default Caption];
[self setPhotographer:@Daniela da Cruz];
}

return self;

49
CLASSES  IMPLEMENTATION
1. Instance variables are set to

nil

automatically, so inside the

brackets, we set default values for

2.

photographer.
Any init method

caption

we create has to return

and

self

at the end.

Our implementation calls to the superclass version of

init

and captures the result, so we need to actually return the new
object to whoever requested it.

50
CLASSES  INIT
Straight from

Apple's ocial documentation:

There are several critical rules to follow when implementing an

init

method that serves as a class's sole initializer or, if there are

multiple initializers, its

designated initializer:

1. Always invoke the superclass (super) initializer rst.
2. Check the object returned by the superclass. If it is
initialization cannot proceed; return

nil

nil,

then

to the receiver.

3. After setting instance variables to valid initial values, return
self.

51
CLASSES  INIT
The basic format of a method to override the initializer looks as
follows:

-(id)init
{
self = [super init];
if (self != nil)
{
// Initialization code here
}
return self;
}

52
CLASSES  DESIGNATED INITIALIZER
When working with a class that has more than one initialization
method, it's important that one of the initializers drives the whole
process.
Put another way,

only one of the initializer methods does the

actual work of calling the super class initializer and initializing the
instance variables of the object.
A general rule of thumb (although not always the case) is that the
designated initializer is the initializer with the most parameters.

53
CLASSES  DESIGNATED INITIALIZER
@implementation Photo
// = Designated initializer =
-(id)initWithCaptionAndPhotographer: (NSString *)inCaption
photographer:(NSString *)inPhotographer
{
if (self = [super init])
{
[self setCaption:inCaption];
[self setPhotographer:inPhotographer];
}
return self;
}

54
CLASSES  DESIGNATED INITIALIZER
-(id)initWithCaption: (NSString *)inCaption
{
return [self initWithCaptionAndPhotographer:inCaption
photographer:nil];
}
-(id)init
{
return [self initWithCaption:nil];
}
@end

55
CLASSES
Some notes:

→

All classes are derived from the base class called

→ NSObject
→

NSObject.

it is the superclass of all Objective-C classes.

It provides basic methods like memory allocation and
initialization.

→

Getters usually don't include the get keyword in method
name.

→

Init always return self  at the end.

56
1. The iOS architecture

2. Objective-C
2.1

History

2.2

Objective-C primitives

2.3

Foundation Framework  Data Types (Part I)

2.4

Pointers

2.5

Classes

2.6

Namespaces

2.7

Utilities

57
NAMESPACES

Objective-C does

not support namespaces, which is why the Cocoa

functions require prexes like NS, CA, AV, etc to avoid naming
collisions.
The recommended convention is to use a three-letter prex for your
application-specic classes (e.g., XYZCar).

58
1. The iOS architecture

2. Objective-C
2.1

History

2.2

Objective-C primitives

2.3

Foundation Framework  Data Types (Part I)

2.4

Pointers

2.5

Classes

2.6

Namespaces

2.7

Utilities

59
UTILITIES  NSLOG
NSLog is used for printing a statement.
It will be printed in device logs and debug console in release and
debug modes respectively.

Format Speciers:
→ %i, %d
→ %u
→ %f

→ %@

 signed integer

 unsigned integer
 oat
 object

60

More Related Content

PPTX
Introduction to Objective - C
PDF
Introduction to objective c
PDF
iOS 101 - Xcode, Objective-C, iOS APIs
PPT
Objective-C for iOS Application Development
PPTX
iOS Basic
PDF
Lec 4 06_aug [compatibility mode]
PPTX
Creating a textual domain specific language
PPT
Pursuing Domain-Driven Design practices in PHP
Introduction to Objective - C
Introduction to objective c
iOS 101 - Xcode, Objective-C, iOS APIs
Objective-C for iOS Application Development
iOS Basic
Lec 4 06_aug [compatibility mode]
Creating a textual domain specific language
Pursuing Domain-Driven Design practices in PHP

What's hot (19)

PPTX
Core java
PPTX
.NET Attributes and Reflection - What a Developer Needs to Know...
PPT
Objective c
PPTX
Intro To C++ - Class 2 - An Introduction To C++
PDF
Qb it1301
PPT
Java basic
PDF
Eero cocoaheadssf-talk
PDF
Pursuing practices of Domain-Driven Design in PHP
DOCX
The Seven Pillars Of Asp.Net
PPTX
Object oriented programming in C++
ZIP
Introduction to the Java(TM) Advanced Imaging API
PPTX
Android ndk - Introduction
PPTX
Java notes(OOP) jkuat IT esection
PDF
C# (This keyword, Properties, Inheritance, Base Keyword)
PDF
Object Oriented Programming using C++ - Part 2
PPT
C++ oop
PPTX
Introduction to JAVA
Core java
.NET Attributes and Reflection - What a Developer Needs to Know...
Objective c
Intro To C++ - Class 2 - An Introduction To C++
Qb it1301
Java basic
Eero cocoaheadssf-talk
Pursuing practices of Domain-Driven Design in PHP
The Seven Pillars Of Asp.Net
Object oriented programming in C++
Introduction to the Java(TM) Advanced Imaging API
Android ndk - Introduction
Java notes(OOP) jkuat IT esection
C# (This keyword, Properties, Inheritance, Base Keyword)
Object Oriented Programming using C++ - Part 2
C++ oop
Introduction to JAVA
Ad

Viewers also liked (20)

DOCX
Java course
PDF
JAVA TITLES 2014
PDF
IEEE PROJECT CENTER IN CHENNAI
PDF
PROJECTS FROM SHPINE TECHNOLOGIES
PDF
Android ieee project titles 2015 2016
PPT
Plagiarism for Faculty Workshop
PPTX
Why publish in an international journal?
DOCX
Dot Net Course Syllabus
PDF
ANDROID IEEE PROJECT TITLES 2014
DOCX
Embedded project titles1:2015-2016
PDF
Matlab titles 2015 2016
PPT
Scopus Overview
PPSX
Android os by jje
PPTX
Marshmallow
PDF
EMBEDDED-MICRO CONTROLLER BASED WIRELESS PROJECTS TITLES2014
PDF
Java titles 2015 2016
PPTX
Material design in android L developer Preview
PPT
Looking for scopus journals
PDF
Understanding Color
DOCX
SOFTWARE MANUAL TESTING SYLLABUS
Java course
JAVA TITLES 2014
IEEE PROJECT CENTER IN CHENNAI
PROJECTS FROM SHPINE TECHNOLOGIES
Android ieee project titles 2015 2016
Plagiarism for Faculty Workshop
Why publish in an international journal?
Dot Net Course Syllabus
ANDROID IEEE PROJECT TITLES 2014
Embedded project titles1:2015-2016
Matlab titles 2015 2016
Scopus Overview
Android os by jje
Marshmallow
EMBEDDED-MICRO CONTROLLER BASED WIRELESS PROJECTS TITLES2014
Java titles 2015 2016
Material design in android L developer Preview
Looking for scopus journals
Understanding Color
SOFTWARE MANUAL TESTING SYLLABUS
Ad

Similar to Introduction to iOS and Objective-C (20)

PDF
201010 SPLASH Tutorial
PPT
Ios development
PDF
Write native iPhone applications using Eclipse CDT
PDF
iOS Development - Offline Class for Jasakomer
PPTX
Dependency Injection and Autofac
PDF
200910 - iPhone at OOPSLA
PPTX
Ios fundamentals with ObjectiveC
PDF
iOS overview
PPTX
Basic Introduction to C++.pptx
PDF
SpringPeople Introduction to iOS Apps Development
PDF
ISS Art. How to do IT. Kotlin Multiplatform
PDF
A Hand Book of Visual Basic 6.0.pdf.pdf
PPTX
iphone application development
PDF
02 objective-c session 2
PPTX
iOS,From Development to Distribution
PPTX
iOS Course day 2
PDF
T2
DOCX
Learning activity 3
PPTX
An introduction to object-oriented programming.pptx
PDF
programacion orientado a abjetos poo
201010 SPLASH Tutorial
Ios development
Write native iPhone applications using Eclipse CDT
iOS Development - Offline Class for Jasakomer
Dependency Injection and Autofac
200910 - iPhone at OOPSLA
Ios fundamentals with ObjectiveC
iOS overview
Basic Introduction to C++.pptx
SpringPeople Introduction to iOS Apps Development
ISS Art. How to do IT. Kotlin Multiplatform
A Hand Book of Visual Basic 6.0.pdf.pdf
iphone application development
02 objective-c session 2
iOS,From Development to Distribution
iOS Course day 2
T2
Learning activity 3
An introduction to object-oriented programming.pptx
programacion orientado a abjetos poo

More from Daniela Da Cruz (9)

PDF
Games Concepts
PDF
C basics
PDF
Game Development with AndEngine
PDF
Interactive Verification of Safety-Critical Systems
PDF
Android Introduction
PDF
Android Lesson 3 - Intent
PDF
Comment Analysis approach for Program Comprehension (Software Engineering Wor...
PDF
Android Lesson 2
PDF
Android Introduction - Lesson 1
Games Concepts
C basics
Game Development with AndEngine
Interactive Verification of Safety-Critical Systems
Android Introduction
Android Lesson 3 - Intent
Comment Analysis approach for Program Comprehension (Software Engineering Wor...
Android Lesson 2
Android Introduction - Lesson 1

Recently uploaded (20)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Types and Its function , kingdom of life
PDF
RMMM.pdf make it easy to upload and study
PPTX
Institutional Correction lecture only . . .
PPTX
Cell Structure & Organelles in detailed.
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
master seminar digital applications in india
102 student loan defaulters named and shamed – Is someone you know on the list?
STATICS OF THE RIGID BODIES Hibbelers.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
O5-L3 Freight Transport Ops (International) V1.pdf
VCE English Exam - Section C Student Revision Booklet
Cell Types and Its function , kingdom of life
RMMM.pdf make it easy to upload and study
Institutional Correction lecture only . . .
Cell Structure & Organelles in detailed.
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Week 4 Term 3 Study Techniques revisited.pptx
Pre independence Education in Inndia.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
human mycosis Human fungal infections are called human mycosis..pptx
master seminar digital applications in india

Introduction to iOS and Objective-C

  • 1. TECNOLOGIAS EMERGENTES EM JOGOS Engenharia em Desenvolvimento de Jogos Digitais - 2013/2014 February 18, 2014 Daniela da Cruz
  • 2. AGENDA 1. The iOS architecture 2
  • 3. AGENDA 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 2
  • 5. THE IOS ARCHITECTURE → iOS is the operating system that runs on iPad, iPhone, and iPod touch devices. → At the highest level, iOS acts as an intermediary between the underlying hardware and the apps we create. → Apps do not talk to the underlying hardware directly. Instead, they communicate with the hardware through a set of well-dened system interfaces. → These interfaces make it easy to write apps that work consistently on devices having dierent hardware capabilities. 4
  • 6. THE IOS ARCHITECTURE The implementation of iOS technologies can be viewed as a set of layers. 5
  • 7. THE IOS ARCHITECTURE → Lower layers contain fundamental services and technologies. → Higher-level layers build upon the lower layers and provide more sophisticated services and technologies. 6
  • 8. THE IOS ARCHITECTURE CORE OS The Core OS layer contains the low-level features that most other technologies are built upon. 7
  • 9. THE IOS ARCHITECTURE CORE OS The layer provides a variety of services including: → low level networking; → access to external accessories; → and the usual fundamental operating system services such as memory management, le system handling and threads. 8
  • 10. THE IOS ARCHITECTURE CORE SERVICES The iOS Core Services layer provides much of the foundation on which the other layers are built. 9
  • 11. THE IOS ARCHITECTURE CORE SERVICES Key among these services are the Core Foundation and Foundation frameworks, which dene the basic types that all apps use. This layer also contains individual technologies to support features such as location, iCloud, social media, and networking. 10
  • 12. THE IOS ARCHITECTURE MEDIA The Media layer contains the graphics, audio, and video technologies you use to implement multimedia experiences in our apps. The technologies in this layer make easier to build apps that look and sound great. 11
  • 13. THE IOS ARCHITECTURE COCOA The Cocoa Touch layer contains key frameworks for building iOS apps. These frameworks dene the appearance of your app. They also provide the basic app infrastructure and support for key technologies such as multitasking, touch-based input, push notications, and many high-level system services. 12
  • 15. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 14
  • 16. HISTORICAL CONTEXT → 1972 → 1980 → 1983 → 1986 C programming language SmallTalk (rst OO language) C++ (C extension with OO support) Objective-C (C extension with concepts from SmallTalk) 15
  • 17. WHY OBJECTIVE-C? → Was chosen as the main language in NeXT company founded by Steve Jobs to develop the operating System NeXTStep → In 1996, Apple buys NeXT and starting the development of Mac OS X (2001) using Objective-C Objective-C is the native programming language for Apple's iOS and OS X operating systems. 16
  • 18. OBJECTIVE-C It's a compiled, general-purpose language capable of building everything from command line utilities to animated GUIs to domain-specic libraries. 17
  • 19. OBJECTIVE-C Objective-C is a strict superset of C, which means that it's possible to seamlessly combine both languages in the same source le. In fact, Objective-C relies on C for most of its core language t's important to have at least a basic foundation in C before tackling the higher-level aspects of the constructs, so i language. 18
  • 20. OBJECTIVE-C Like C++, Objective-C was designed to add object-oriented features to C, but the two languages accomplished this using fundamentally distinct philosophies. → Objective-C is more dynamic, deferring most of its decisions to run-time rather than compile-time. → Objective-C is also known for its verbose naming conventions. 19
  • 21. OBJECTIVE-C C++ 1 john-drive(Corvette, Mary's House) Objective-C 1 [john driveCar:@Corvette toDestination:@Mary's House] Objective-C method calls read more like a human language than a computer one. 20
  • 22. OBJECTIVE-C As with plain old C programs, the main() function serves as the root of an Objective-C application. An autoreleasepool implements simple garbage collection for reference-counted objects. It temporarily takes ownwership of reference-counted objects that nobody else wants to take ownership of and releases them at a later, appropriate point in time. 21
  • 23. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 22
  • 24. ID TYPE The id type is the generic type for all Objective-C objects. You can think of it as the object-oriented version of C's void pointer (void*). And, like a void pointer, it can store a reference to any type of object. id mysteryObject = @An NSString object; 23
  • 25. CLASS TYPE Objective-C classes are represented as objects themselves, using a special data type called Class. This lets you, for example, dynamically check an object's type at runtime (introspection for later discussion). Class targetClass = [NSString class]; id mysteryObject = @An NSString object; if ([mysteryObject isKindOfClass:targetClass]) { NSLog(@Yup! That's an instance of the target class); } 24
  • 26. SEL TYPE The SEL data type is used to store selectors, which are Objective-C's internal representation of a method name. For example, the following snippet stores a method called sayHello in the someMethod variable. This variable could be used to dynamically call a method at runtime. SEL someMethod = @selector(sayHello); 25
  • 27. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 26
  • 28. FOUNDATION FRAMEWORK DATA TYPES (PART I) NSObject is the root class of most Objective-C class hierarchies. Is the base class for pretty much every object in the iOS SDK. 27
  • 29. FOUNDATION FRAMEWORK DATA TYPES BOOL used for representing Boolean (YES or NO are BOOL types allowed). BOOL myBOOLyes = YES; BOOL myBOOLno = NO; 28
  • 30. FOUNDATION FRAMEWORK DATA TYPES NSString used for representing a string instead of C language's char* → → type. An NSString instance immutable. can not be modied! They are Tons of utility functions available (case conversion, URLs, substrings, type conversions, etc.). NSString* hi = @Hi; 29
  • 31. FOUNDATION FRAMEWORK DATA TYPES NSMutableString Mutable version of NSString. (Somewhat rarely used.) NSMutableString * str1 = [NSMutableString stringWithString:@Hello World]; 30
  • 32. FOUNDATION FRAMEWORK DATA TYPES NSNumber Object wrapper around primitive types like int, float, double, BOOL, etc. NSNumber *num = [NSNumber numberWithInt:36]; float f = [num floatValue]; // would return 36 as a float // (i.e. will convert types) 31
  • 33. FOUNDATION FRAMEWORK DATA TYPES NSValue Generic object wrapper for other non-object data types. // CGPoint is a C struct CGPoint point = CGPointMake(25.0, 15.0); NSValue *pointObject = [NSValue valueWithCGPoint:point]; 32
  • 34. FOUNDATION FRAMEWORK DATA TYPES NSDate Used to nd out the time right now or to store past or future times/dates. An NSDate object represents a specic point in time, independent of any particular calendrical system, time zone, or locale. Internally, it just records the number of seconds from an arbitrary reference point (January 1st, 2001 GMT). NSDate *now = [NSDate date]; 33
  • 35. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 34
  • 36. POINTERS All Objective-C objects are referenced as pointers. An NSString object must be stored as a pointer, not a normal variable: NSString *model = @Honda; 35
  • 37. POINTERS There is a slight dierence between C and Objective-C. Whereas C uses NULL, Objective-C denes its own macro, nil, as its null object. NSString *anObject; anObject = NULL; anObject = nil; // An Objective-C object // This will work // But this is preferred int *aPointer; aPointer = nil; aPointer = NULL; // A plain old C pointer // Don't do this // Do this instead 36
  • 38. NIL VERSUS NULL Objective-C's nill keyword is similar to NULL in other languages like C and Java. The dierence is that it is perfectly safe to call methods on If any method that returns an object is called on receive a nil nill, nil. we will back. 37
  • 39. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 38
  • 40. CLASSES When we dene a class, we dene a blueprint for a data type. This doesn't actually dene any data, but it does dene what the class name means, that is: → what an object of the class will consist of; and → what operations can be performed on such an object. 39
  • 41. CLASSES INTERFACE An interface declares the public properties and methods of a class. @interface Photo : NSObject { // Protected instance variables (not recommended) } // Properties and Methods @end Protected variables can be dened inside of the curly braces, but most developers treat instance variables as implementation details and prefer to store them in the .m le instead of the interface. 40
  • 42. CLASSES INTERFACE When declaring a method, ask yourself: Does the method apply to only one instance or all instances of the class? If it applies to all instances, it's usually better as a class method. Class methods are preceded by a minus (-) symbol. Instance methods are preceded by a plus (+) symbol. → 41
  • 43. CLASSES INTERFACE Example: @interface Photo : NSObject - (NSString*) caption; - (NSString*) photographer; - (void) setCaption: (NSString*)input; - (void) setPhotographer: (NSString*)input; @end 42
  • 44. CLASSES IMPLEMENTATION The @implementation directive is similar to @interface, except you don't need to include the super class. @implementation Photo { // Private instance variables } // Methods @end The instance variables are private and are only accessible inside the class implementation. 43
  • 45. CLASSES IMPLEMENTATION Continuation of previous example: @implementation Photo { NSString* caption; NSString* photographer; } - (NSString*) caption { return caption; } - (NSString*) photographer { return photographer; } @end 44
  • 46. CLASSES IMPLEMENTATION The getters themselves are very simple. They just return the instance variable. A setter's job is to swap the old value with the new one. Objective-C objects are dierent than an int or float. assign an object to a variable, it does not get the reference to the object. When you value; it gets a 45
  • 47. CLASSES IMPLEMENTATION - (void) setCaption: (NSString*) input { [caption autorelease]; [input retain]; caption = input; } 46
  • 48. CLASSES IMPLEMENTATION 1. [caption autorelease] We no longer need the old NSString object that caption refers so, so we call autorelease on it. 2. [input retain] We call retain on the input object because we are going to assign it to the instance variable and need it to stay around. 3. caption = input Finally we set the caption instance variable to be equal to input. 47
  • 49. DO I ALWAYS NEED TO DO THIS? NO but only if you choose Automatic Reference Counting (ARC) to be used in your project!!! 48
  • 50. CLASSES IMPLEMENTATION Classes usually have an init method to set initial values for its instance variables or other setup tasks. - (id) init { [self setCaption:@Default Caption]; [self setPhotographer:@Daniela da Cruz]; } return self; 49
  • 51. CLASSES IMPLEMENTATION 1. Instance variables are set to nil automatically, so inside the brackets, we set default values for 2. photographer. Any init method caption we create has to return and self at the end. Our implementation calls to the superclass version of init and captures the result, so we need to actually return the new object to whoever requested it. 50
  • 52. CLASSES INIT Straight from Apple's ocial documentation: There are several critical rules to follow when implementing an init method that serves as a class's sole initializer or, if there are multiple initializers, its designated initializer: 1. Always invoke the superclass (super) initializer rst. 2. Check the object returned by the superclass. If it is initialization cannot proceed; return nil nil, then to the receiver. 3. After setting instance variables to valid initial values, return self. 51
  • 53. CLASSES INIT The basic format of a method to override the initializer looks as follows: -(id)init { self = [super init]; if (self != nil) { // Initialization code here } return self; } 52
  • 54. CLASSES DESIGNATED INITIALIZER When working with a class that has more than one initialization method, it's important that one of the initializers drives the whole process. Put another way, only one of the initializer methods does the actual work of calling the super class initializer and initializing the instance variables of the object. A general rule of thumb (although not always the case) is that the designated initializer is the initializer with the most parameters. 53
  • 55. CLASSES DESIGNATED INITIALIZER @implementation Photo // = Designated initializer = -(id)initWithCaptionAndPhotographer: (NSString *)inCaption photographer:(NSString *)inPhotographer { if (self = [super init]) { [self setCaption:inCaption]; [self setPhotographer:inPhotographer]; } return self; } 54
  • 56. CLASSES DESIGNATED INITIALIZER -(id)initWithCaption: (NSString *)inCaption { return [self initWithCaptionAndPhotographer:inCaption photographer:nil]; } -(id)init { return [self initWithCaption:nil]; } @end 55
  • 57. CLASSES Some notes: → All classes are derived from the base class called → NSObject → NSObject. it is the superclass of all Objective-C classes. It provides basic methods like memory allocation and initialization. → Getters usually don't include the get keyword in method name. → Init always return self at the end. 56
  • 58. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 57
  • 59. NAMESPACES Objective-C does not support namespaces, which is why the Cocoa functions require prexes like NS, CA, AV, etc to avoid naming collisions. The recommended convention is to use a three-letter prex for your application-specic classes (e.g., XYZCar). 58
  • 60. 1. The iOS architecture 2. Objective-C 2.1 History 2.2 Objective-C primitives 2.3 Foundation Framework Data Types (Part I) 2.4 Pointers 2.5 Classes 2.6 Namespaces 2.7 Utilities 59
  • 61. UTILITIES NSLOG NSLog is used for printing a statement. It will be printed in device logs and debug console in release and debug modes respectively. Format Speciers: → %i, %d → %u → %f → %@ signed integer unsigned integer oat object 60