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

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 

Kürzlich hochgeladen (20)

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
TEST BANK For, Information Technology Project Management 9th Edition Kathy Sc...
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 

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/