SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
Getting Started with
Objective-C and Xcode
What is Objective-C?
•

Objective-C is an object-oriented language, built on top of C

•

Objective-C adds Smalltalk-style message passing to C

•

It is also the default language used to program for Mac OS X
and iOS devices.
Message Passing? What’s
that about?
•

You can think of message passing as calling a method… well
almost

•

When you pass a message to an object in Objective-C, you’re
asking that object to look up the implementation of a method
of the same name as your method.

•

When that object finds an implementation, it calls that method.

•

The receiver of the message is also not guaranteed to respond
to the message at all
Anything else I need to
know?
•

Yeah! Don’t get scared by the syntax!

•

At first, all the square brackets can freak people out.

•

Once you get used to reading the messages, you’ll wonder
how you ever understood a Java method call or C function
call in the first place. (Kidding, kindof)

•

Let’s look at some code
Java: Person Class
public Class Person{

!

!

public Person(String n,
int a, Date b){
this.name = n;
this.age = a;
this.birthday = b;
}
}

private String name;
private int age;
private Date birthday;
public Person(){
name = “”;
age = 0;
birthday = null;
}
!
!
Objective-C Person Class
(Header)

@interface Person : NSObject

@property (strong, nonatomic) NSString name;
@property (nonatomic) int age;
@property (strong, nonatomic) NSDate birthday;
- (id)init;
- (id)initWithName:(NSString *)name age:(int)age (NSDate *)birthday;
!

@end
Objective-C Person Class
(Implementation File)

@implementation Person

}

!

!

//properties have getters and
setters synthizised for you, but
I’ll show you one

- (id)init{
self.name = @“”;
self.age = 0;
self.birthday = nil;
}

!

- (void)setName:(NSString *)name{
//self.name = name will
cause and infinite loop
//why?
_name = name;
}
!

- (NSString *)name{
return _name;

!

- (id)initWithName:(NSString
*)name age:(int)age (NSDate
*)birthday{

self.name = name;
self.age = age;
self.birthday = birthday;
}
A few small notes
•

Do I have to manage my own memory with Objective-C? It is C after all...
Not anymore. You have the option of running a Garbage Collector or using Automatic
Reference Counting (which is way awesome). You could also manage your own memory
with Manual Reference Counting

•

What's that "id" thing on the last screen?
id holds any pointer to any object in Objective-C. Think about it like a void *

•

Every Functional Programmer's favorite question: Does it have closures?First class
functions?
Yes! (and no) Objective-C has blocks, which are part of LLVM and Clang. I'm sure
someone will fight me that these are real closures, but they're pretty close. You can also
pass them around to like first class functions (sort of)
Does @interface create a
Java-like interface?
•

Short answer is no.

•

@interface declares the methods/properties/variables that you’re going
to have available in your class. There is an option to include a private
@interface in your .m file

•

In Objective-C, Java-like interfaces are called Protocols

•

You define them with @protocol, and can specify optional and required
methods.

•

You declare that you implemente a protocol like this:

@interface Person : NSObject <MyProtocolHere>
One More Thing:
Categories
•

Categories provide a way for you to customize an existing class that you didn't write

•

This works pretty much the same way as C# Extension methods if you're familiar with those.

•

Why do you want to use this? What if you want to add a method to a built in class that adds
functionality without subclassing?

•

Here's what categories look like:
@interface UIColor : NSObject (HexColor)
+ (UIColor *)colorWithHex:(NSString *)hexCode;
@end

•

We usually put this in a file called UIColor+HexColor.h and anywhere you import that file,
colorWithHex: will be available. Anywhere we don't, we don't get that method.
Live Demo Time
Other Cool Things to
Research

•

Model View Controller

•

Delegation

•

Blocks

•

Key Value Observing / Key Value Coding

•

The Objective-C Runtime

•

Class Posing (advanced)

•

Method Swizzling (advanced)
Great Resources
•

Apple Developer Site - https://developer.apple.com

•

Big Nerd Ranch iOS Programming Book http://www.bignerdranch.com/book/
ios_programming_the_big_nerd_ranch_guide_rd_edition_

•

NSHipster - http://nshipster.com

•

Stanford iOS Course - https://itunes.apple.com/us/course/developingios-7-apps-for/id733644550 (recently updated)

•

NSScreencast - http://nsscreencast.com/

Weitere ähnliche Inhalte

Was ist angesagt?

Intro to Objective C
Intro to Objective CIntro to Objective C
Intro to Objective C
Ashiq Uz Zoha
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
rahulsahay19
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
DataArt
 
OOP programming
OOP programmingOOP programming
OOP programming
anhdbh
 
Executable specifications for xtext
Executable specifications for xtextExecutable specifications for xtext
Executable specifications for xtext
meysholdt
 

Was ist angesagt? (20)

Java Programming - Polymorphism
Java Programming - PolymorphismJava Programming - Polymorphism
Java Programming - Polymorphism
 
Lec08 constructors
Lec08   constructorsLec08   constructors
Lec08 constructors
 
Intro to Objective C
Intro to Objective CIntro to Objective C
Intro to Objective C
 
C# Summer course - Lecture 1
C# Summer course - Lecture 1C# Summer course - Lecture 1
C# Summer course - Lecture 1
 
introduction to c #
introduction to c #introduction to c #
introduction to c #
 
Classes And Objects
Classes And ObjectsClasses And Objects
Classes And Objects
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
 
03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop03 classes interfaces_principlesofoop
03 classes interfaces_principlesofoop
 
Objected-Oriented Programming with Java
Objected-Oriented Programming with JavaObjected-Oriented Programming with Java
Objected-Oriented Programming with Java
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
Typescript
TypescriptTypescript
Typescript
 
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
Advanced Object Oriented JavaScript (prototype, closure, scope, design patterns)
 
OOP programming
OOP programmingOOP programming
OOP programming
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphism
 
Java Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract ClassJava Programming - Introduction to Abstract Class
Java Programming - Introduction to Abstract Class
 
Executable specifications for xtext
Executable specifications for xtextExecutable specifications for xtext
Executable specifications for xtext
 
PHP - Procedural To Object-Oriented
PHP - Procedural To Object-OrientedPHP - Procedural To Object-Oriented
PHP - Procedural To Object-Oriented
 

Andere mochten auch

Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
sagaroceanic11
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
Muhammad Abdullah
 

Andere mochten auch (13)

Programming with Objective-C
Programming with Objective-CProgramming with Objective-C
Programming with Objective-C
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
Embedded Objective-C
Embedded Objective-CEmbedded Objective-C
Embedded Objective-C
 
Crash Course in Objective-C
Crash Course in Objective-CCrash Course in Objective-C
Crash Course in Objective-C
 
Objective-c for Java Developers
Objective-c for Java DevelopersObjective-c for Java Developers
Objective-c for Java Developers
 
Runtime
RuntimeRuntime
Runtime
 
Objective-C for Java Developers
Objective-C for Java DevelopersObjective-C for Java Developers
Objective-C for Java Developers
 
Construction laws kazakhstan 2105
Construction laws kazakhstan 2105Construction laws kazakhstan 2105
Construction laws kazakhstan 2105
 
El Estrés Laboral
El Estrés LaboralEl Estrés Laboral
El Estrés Laboral
 
Open Education Resources (OER) Differentiation in Africa (Kenya, Ghana and S...
Open Education Resources (OER) Differentiation in Africa  (Kenya, Ghana and S...Open Education Resources (OER) Differentiation in Africa  (Kenya, Ghana and S...
Open Education Resources (OER) Differentiation in Africa (Kenya, Ghana and S...
 
Recurso unidad 4
Recurso unidad 4Recurso unidad 4
Recurso unidad 4
 
Báo cáo phân tích cổ phiếu Traphaco tháng 9/2013
Báo cáo phân tích cổ phiếu Traphaco tháng 9/2013Báo cáo phân tích cổ phiếu Traphaco tháng 9/2013
Báo cáo phân tích cổ phiếu Traphaco tháng 9/2013
 
Trousse Pédagogique Le Robert junior - 1 er cycle du primaire
Trousse Pédagogique Le Robert junior - 1 er cycle du primaireTrousse Pédagogique Le Robert junior - 1 er cycle du primaire
Trousse Pédagogique Le Robert junior - 1 er cycle du primaire
 

Ähnlich wie Objective-C talk

Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
Vu Tran Lam
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
Sunny Shaikh
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
Petr Dvorak
 

Ähnlich wie Objective-C talk (20)

Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Objective c
Objective cObjective c
Objective c
 
Objective-C Is Not Java
Objective-C Is Not JavaObjective-C Is Not Java
Objective-C Is Not Java
 
Objective c intro (1)
Objective c intro (1)Objective c intro (1)
Objective c intro (1)
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Cocoa for Web Developers
Cocoa for Web DevelopersCocoa for Web Developers
Cocoa for Web Developers
 
iOS Application Development
iOS Application DevelopmentiOS Application Development
iOS Application Development
 
Ios development
Ios developmentIos development
Ios development
 
01 objective-c session 1
01  objective-c session 101  objective-c session 1
01 objective-c session 1
 
Дмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI векеДмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI веке
 
Iphone course 1
Iphone course 1Iphone course 1
Iphone course 1
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
MFF UK - Introduction to iOS
MFF UK - Introduction to iOSMFF UK - Introduction to iOS
MFF UK - Introduction to iOS
 
Oop java
Oop javaOop java
Oop java
 
Introduction to c ++ part -1
Introduction to c ++   part -1Introduction to c ++   part -1
Introduction to c ++ part -1
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
 
Learn c sharp at amc square learning
Learn c sharp at amc square learningLearn c sharp at amc square learning
Learn c sharp at amc square learning
 
Objective-C Crash Course for Web Developers
Objective-C Crash Course for Web DevelopersObjective-C Crash Course for Web Developers
Objective-C Crash Course for Web Developers
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
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
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 

Objective-C talk

  • 2. What is Objective-C? • Objective-C is an object-oriented language, built on top of C • Objective-C adds Smalltalk-style message passing to C • It is also the default language used to program for Mac OS X and iOS devices.
  • 3. Message Passing? What’s that about? • You can think of message passing as calling a method… well almost • When you pass a message to an object in Objective-C, you’re asking that object to look up the implementation of a method of the same name as your method. • When that object finds an implementation, it calls that method. • The receiver of the message is also not guaranteed to respond to the message at all
  • 4. Anything else I need to know? • Yeah! Don’t get scared by the syntax! • At first, all the square brackets can freak people out. • Once you get used to reading the messages, you’ll wonder how you ever understood a Java method call or C function call in the first place. (Kidding, kindof) • Let’s look at some code
  • 5. Java: Person Class public Class Person{ ! ! public Person(String n, int a, Date b){ this.name = n; this.age = a; this.birthday = b; } } private String name; private int age; private Date birthday; public Person(){ name = “”; age = 0; birthday = null; } ! !
  • 6. Objective-C Person Class (Header) @interface Person : NSObject @property (strong, nonatomic) NSString name; @property (nonatomic) int age; @property (strong, nonatomic) NSDate birthday; - (id)init; - (id)initWithName:(NSString *)name age:(int)age (NSDate *)birthday; ! @end
  • 7. Objective-C Person Class (Implementation File) @implementation Person } ! ! //properties have getters and setters synthizised for you, but I’ll show you one - (id)init{ self.name = @“”; self.age = 0; self.birthday = nil; } ! - (void)setName:(NSString *)name{ //self.name = name will cause and infinite loop //why? _name = name; } ! - (NSString *)name{ return _name; ! - (id)initWithName:(NSString *)name age:(int)age (NSDate *)birthday{
 self.name = name; self.age = age; self.birthday = birthday; }
  • 8. A few small notes • Do I have to manage my own memory with Objective-C? It is C after all... Not anymore. You have the option of running a Garbage Collector or using Automatic Reference Counting (which is way awesome). You could also manage your own memory with Manual Reference Counting • What's that "id" thing on the last screen? id holds any pointer to any object in Objective-C. Think about it like a void * • Every Functional Programmer's favorite question: Does it have closures?First class functions? Yes! (and no) Objective-C has blocks, which are part of LLVM and Clang. I'm sure someone will fight me that these are real closures, but they're pretty close. You can also pass them around to like first class functions (sort of)
  • 9. Does @interface create a Java-like interface? • Short answer is no. • @interface declares the methods/properties/variables that you’re going to have available in your class. There is an option to include a private @interface in your .m file • In Objective-C, Java-like interfaces are called Protocols • You define them with @protocol, and can specify optional and required methods. • You declare that you implemente a protocol like this:
 @interface Person : NSObject <MyProtocolHere>
  • 10. One More Thing: Categories • Categories provide a way for you to customize an existing class that you didn't write • This works pretty much the same way as C# Extension methods if you're familiar with those. • Why do you want to use this? What if you want to add a method to a built in class that adds functionality without subclassing? • Here's what categories look like: @interface UIColor : NSObject (HexColor) + (UIColor *)colorWithHex:(NSString *)hexCode; @end • We usually put this in a file called UIColor+HexColor.h and anywhere you import that file, colorWithHex: will be available. Anywhere we don't, we don't get that method.
  • 12. Other Cool Things to Research • Model View Controller • Delegation • Blocks • Key Value Observing / Key Value Coding • The Objective-C Runtime • Class Posing (advanced) • Method Swizzling (advanced)
  • 13. Great Resources • Apple Developer Site - https://developer.apple.com • Big Nerd Ranch iOS Programming Book http://www.bignerdranch.com/book/ ios_programming_the_big_nerd_ranch_guide_rd_edition_ • NSHipster - http://nshipster.com • Stanford iOS Course - https://itunes.apple.com/us/course/developingios-7-apps-for/id733644550 (recently updated) • NSScreencast - http://nsscreencast.com/