際際滷

際際滷Share a Scribd company logo
A Quick and Dirty Intro to
Objective C and iPhone
Programming
or how I stopped worrying and learned to love Steve Jobs
What were going to cover
 Objective C basics
 iPhone library basics
 Writing an app
 Worshipping Steve Jobs
Objective C
 Another way of doing object oriented C
 Superset of the C language
 Uses the Smalltalk idea of message
passing rather than method invocation
 May look familiar to Vision users
Objective C
 Created in the early 80s
 Adopted by NeXT in 1988
 Apple bought NeXT in 1996 and adopted
objective C as the language behind Mac
OS X
HAIL STEVE
KING OF NeXT!
Message Passing
 Instead of calling a function we send
messages to our object;
Foo *myObject;
...
[myObject doThingsWithArg:argument];
Object Selector Argument
Function Declaration
 More new syntax:
-(void) doThingsWithArg:(int)anArg;
Method
scope
Return Type Selector Argument type Argument name
Class Definitions
 Split into two files
.h  header
.m - implementation
Foo.h:
@interface Foo : NSObject {
int anInt;
NSString *aString;
}
-(void) doThingsWithArg:(int)anArg;
@end
Foo.m:
#import Foo.h
@implementation Foo
-(void) doThingsWithArg:(int)anArg {

}
@end
Constructors
 2 part construction of objects
 Allocation: Allocation selector is alloc
 Initialisation: Initialisation selector is custom,
but always starts with init by convention.
 Constructors return id type
 Standard construction line looks like:
[[myObject alloc] init];
Foo.h:
@interface Foo : NSObject {
int anInt;
NSString *aString;
}
-(id) init;
-(id) initWithArg:(int)arg
andArg:(NSString*)arg2;
-(void) doThingsWithArg:(int)anArg;
@end
Foo.m:
#import Foo.h
@implentation Foo
-(id) init {
return self;
}
-(id) initWithArg:(int)arg
andArg:(NSString*)arg2
{
anInt=arg;
aString=arg2;
return self;
}
-(void) doThingsWithArg:(int)anArg {}
@end
Properties
 New in Objective C 2
 Released a couple of years back
 Automatic construction of accessors
 Must be declared in header and
synthesised in implementation
Foo.h:
@interface Foo : NSObject {
int anInt;
NSString *aString;
}
-(id) init;
-(id) initWithArg:(int)arg
andArg:(NSString*)arg2;
-(void) doThingsWithArg:(int)anArg;
@property (readwrite,assign) int anInt;
@property (readwrite,copy) NSString *aString;
@end
Foo.m:
#import Foo.h
@implentation Foo
@synthesize anInt;
@synthesize aString;
-(id) init {
[super init];
return self;
}
-(id) initWithArg:(int)arg
andArg:(NSString*)arg2
{
[super init];
myInstanceVar=arg;
aString=arg2;
return self;
}
-(void) doThingsWithArg:(int)anArg {}
@end
Using our new object
Foo *myObj1=[[Foo alloc] init];
Foo *myObj2=[[Foo alloc] initWithArg:1
andArg:@Pie];
[myObj1 doStuffWithArg:23];
NSLog(@%d and %@,myObj2.anInt, myObj2.aString);
//prints out 1 and Pie
NSLog(@%d and %@, [myObj2 getAnInt],
[myObj2 getAString]);
//also prints out 1 and Pie
What was that assign/copy stuff?
 Objective C has a number of ways of
doing memory management
 Garbage collector
 New and not used everywhere
 Allocation pools
 Baby version of garbage collection
 Reference counting
 Used everywhere, including with pools and
garbage collection
What was that assign/copy stuff?
 When you get an object its reference
count is 1
 Use retain to add one to the count
 Use release to drop one from the count
 When count hits zero the object is
destroyed
 Destructor method is called dealloc
A quick and dirty intro to objective c
What was that assign/copy stuff?
 In properties you get to say how you want
the reference counts done
 Assign is a simple assignment, no reference
counting
 Retain returns the pointer to an object and
ups the reference count by 1 (for non-objects
this just works like assign)
 Copy returns a copy of the object
Reference counting
 This talk is called quick and dirty
because thats all the memory
management Im mentioning
 Im still not sure when to retain things as
whether things are copied/retained/etc is
generally based on function naming
conventions.
 Lame
iPhone
 The iPhone uses Objective C and a bunch
of Apple libraries
 For many apps you can just bolt together
library bits with a little bit of extra logic and
data
The Steve says LETS MAKE AN APP
Going Further
 THE Cocoa programming book seems to
be:
Cocoa Programming for Mac OS X by Aaron Hillegass
Going Further
 Series of talks by Evan Doll and Alan
Cannistraro up on iTunes U
 Talks done at Stanford University in
Summer 2009 for the 2nd
year of their
iPhone programming course
 Missing some iPhone OS 3 features, but
very good.
Going Further
 Buy a Mac
 Make The Steve pleased
 Wear rollneck sweaters
 Conform
 Conform
 Conform
 Conform
 Conform
 Conform
 Conform
A quick and dirty intro to objective c
Ad

Recommended

Pa1 functions
Pa1 functions
aiclub_slides
Formatting ForThe Masses
Formatting ForThe Masses
Holger Schill
Python and web driver
Python and web driver
Colm Harrington
Functional Programming in Go
Functional Programming in Go
Aaron Schlesinger
The Bestiary of Pure Functional Programming
The Bestiary of Pure Functional Programming
Garth Gilmour
Extending Python with ctypes
Extending Python with ctypes
Anant Narayanan
C Types - Extending Python
C Types - Extending Python
Priyank Kapadia
Reference Semantik mit C# und .NET Core - BASTA 2019
Reference Semantik mit C# und .NET Core - BASTA 2019
Christian Nagel
Slicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPI
lestrrat
What's new in C# 8.0 (beta)
What's new in C# 8.0 (beta)
Muralidharan Deenathayalan
FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT
ginriki
Zpugdccherry 101105081729-phpapp01
Zpugdccherry 101105081729-phpapp01
Jeffrey Clark
Local search-based pattern matching features in EMF-IncQuery
Local search-based pattern matching features in EMF-IncQuery
Zolt叩n Ujhelyi
Take Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play Framework
Asher Glynn
Declarative authorization in REST services in SharePoint with F# and ServiceS...
Declarative authorization in REST services in SharePoint with F# and ServiceS...
Sergey Tihon
Objective c
Objective c
ricky_chatur2005
Objective c intro (1)
Objective c intro (1)
David Echeverria
亳从亳舒 仂舒亞亳仆 - Programming Apple iOS with Objective-C
亳从亳舒 仂舒亞亳仆 - Programming Apple iOS with Objective-C
DataArt
Objective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
Objective-C
Objective-C
Abdlhadi Oul
iOS Session-2
iOS Session-2
Hussain Behestee
iOS Programming Intro
iOS Programming Intro
Lou Loizides
iOS Application Development
iOS Application Development
Compare Infobase Limited
Iphone course 1
Iphone course 1
Janet Huang
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
Lou Loizides
Objective c slide I
Objective c slide I
Diksha Bhargava
01 objective-c session 1
01 objective-c session 1
Amr Elghadban (AmrAngry)
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
Kyle Oba
Programming with Objective-C
Programming with Objective-C
Nagendra Ram
Cocoa for Web Developers
Cocoa for Web Developers
georgebrock

More Related Content

What's hot (7)

Slicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPI
lestrrat
What's new in C# 8.0 (beta)
What's new in C# 8.0 (beta)
Muralidharan Deenathayalan
FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT
ginriki
Zpugdccherry 101105081729-phpapp01
Zpugdccherry 101105081729-phpapp01
Jeffrey Clark
Local search-based pattern matching features in EMF-IncQuery
Local search-based pattern matching features in EMF-IncQuery
Zolt叩n Ujhelyi
Take Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play Framework
Asher Glynn
Declarative authorization in REST services in SharePoint with F# and ServiceS...
Declarative authorization in REST services in SharePoint with F# and ServiceS...
Sergey Tihon
Slicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPI
lestrrat
FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT
ginriki
Zpugdccherry 101105081729-phpapp01
Zpugdccherry 101105081729-phpapp01
Jeffrey Clark
Local search-based pattern matching features in EMF-IncQuery
Local search-based pattern matching features in EMF-IncQuery
Zolt叩n Ujhelyi
Take Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play Framework
Asher Glynn
Declarative authorization in REST services in SharePoint with F# and ServiceS...
Declarative authorization in REST services in SharePoint with F# and ServiceS...
Sergey Tihon

Similar to A quick and dirty intro to objective c (20)

Objective c
Objective c
ricky_chatur2005
Objective c intro (1)
Objective c intro (1)
David Echeverria
亳从亳舒 仂舒亞亳仆 - Programming Apple iOS with Objective-C
亳从亳舒 仂舒亞亳仆 - Programming Apple iOS with Objective-C
DataArt
Objective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
Objective-C
Objective-C
Abdlhadi Oul
iOS Session-2
iOS Session-2
Hussain Behestee
iOS Programming Intro
iOS Programming Intro
Lou Loizides
iOS Application Development
iOS Application Development
Compare Infobase Limited
Iphone course 1
Iphone course 1
Janet Huang
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
Lou Loizides
Objective c slide I
Objective c slide I
Diksha Bhargava
01 objective-c session 1
01 objective-c session 1
Amr Elghadban (AmrAngry)
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
Kyle Oba
Programming with Objective-C
Programming with Objective-C
Nagendra Ram
Cocoa for Web Developers
Cocoa for Web Developers
georgebrock
Introduction to Objective - C
Introduction to Objective - C
Asim Rais Siddiqui
Ios development
Ios development
elnaqah
Objc
Objc
Pragati Singh
Write native iPhone applications using Eclipse CDT
Write native iPhone applications using Eclipse CDT
Atit Patumvan
Session 2 - Objective-C basics
Session 2 - Objective-C basics
Vu Tran Lam
亳从亳舒 仂舒亞亳仆 - Programming Apple iOS with Objective-C
亳从亳舒 仂舒亞亳仆 - Programming Apple iOS with Objective-C
DataArt
Objective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
iOS Programming Intro
iOS Programming Intro
Lou Loizides
Iphone course 1
Iphone course 1
Janet Huang
Louis Loizides iOS Programming Introduction
Louis Loizides iOS Programming Introduction
Lou Loizides
What Makes Objective C Dynamic?
What Makes Objective C Dynamic?
Kyle Oba
Programming with Objective-C
Programming with Objective-C
Nagendra Ram
Cocoa for Web Developers
Cocoa for Web Developers
georgebrock
Introduction to Objective - C
Introduction to Objective - C
Asim Rais Siddiqui
Ios development
Ios development
elnaqah
Write native iPhone applications using Eclipse CDT
Write native iPhone applications using Eclipse CDT
Atit Patumvan
Session 2 - Objective-C basics
Session 2 - Objective-C basics
Vu Tran Lam
Ad

Recently uploaded (20)

Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
ICT Frame Magazine Pvt. Ltd.
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
Key Requirements to Successfully Implement Generative AI in Edge DevicesOpt...
Key Requirements to Successfully Implement Generative AI in Edge DevicesOpt...
Edge AI and Vision Alliance
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Josef Weingand
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
ICT Frame Magazine Pvt. Ltd.
OWASP Barcelona 2025 Threat Model Library
OWASP Barcelona 2025 Threat Model Library
PetraVukmirovic
Key Requirements to Successfully Implement Generative AI in Edge DevicesOpt...
Key Requirements to Successfully Implement Generative AI in Edge DevicesOpt...
Edge AI and Vision Alliance
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
The Future of Technology: 2025-2125 by Saikat Basu.pdf
The Future of Technology: 2025-2125 by Saikat Basu.pdf
Saikat Basu
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape sch端tzt, was z辰hlt! Und besonders mit dem neust...
Josef Weingand
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
Ad

A quick and dirty intro to objective c

  • 1. A Quick and Dirty Intro to Objective C and iPhone Programming or how I stopped worrying and learned to love Steve Jobs
  • 2. What were going to cover Objective C basics iPhone library basics Writing an app Worshipping Steve Jobs
  • 3. Objective C Another way of doing object oriented C Superset of the C language Uses the Smalltalk idea of message passing rather than method invocation May look familiar to Vision users
  • 4. Objective C Created in the early 80s Adopted by NeXT in 1988 Apple bought NeXT in 1996 and adopted objective C as the language behind Mac OS X
  • 6. Message Passing Instead of calling a function we send messages to our object; Foo *myObject; ... [myObject doThingsWithArg:argument]; Object Selector Argument
  • 7. Function Declaration More new syntax: -(void) doThingsWithArg:(int)anArg; Method scope Return Type Selector Argument type Argument name
  • 8. Class Definitions Split into two files .h header .m - implementation
  • 9. Foo.h: @interface Foo : NSObject { int anInt; NSString *aString; } -(void) doThingsWithArg:(int)anArg; @end
  • 10. Foo.m: #import Foo.h @implementation Foo -(void) doThingsWithArg:(int)anArg { } @end
  • 11. Constructors 2 part construction of objects Allocation: Allocation selector is alloc Initialisation: Initialisation selector is custom, but always starts with init by convention. Constructors return id type Standard construction line looks like: [[myObject alloc] init];
  • 12. Foo.h: @interface Foo : NSObject { int anInt; NSString *aString; } -(id) init; -(id) initWithArg:(int)arg andArg:(NSString*)arg2; -(void) doThingsWithArg:(int)anArg; @end
  • 13. Foo.m: #import Foo.h @implentation Foo -(id) init { return self; } -(id) initWithArg:(int)arg andArg:(NSString*)arg2 { anInt=arg; aString=arg2; return self; } -(void) doThingsWithArg:(int)anArg {} @end
  • 14. Properties New in Objective C 2 Released a couple of years back Automatic construction of accessors Must be declared in header and synthesised in implementation
  • 15. Foo.h: @interface Foo : NSObject { int anInt; NSString *aString; } -(id) init; -(id) initWithArg:(int)arg andArg:(NSString*)arg2; -(void) doThingsWithArg:(int)anArg; @property (readwrite,assign) int anInt; @property (readwrite,copy) NSString *aString; @end
  • 16. Foo.m: #import Foo.h @implentation Foo @synthesize anInt; @synthesize aString; -(id) init { [super init]; return self; } -(id) initWithArg:(int)arg andArg:(NSString*)arg2 { [super init]; myInstanceVar=arg; aString=arg2; return self; } -(void) doThingsWithArg:(int)anArg {} @end
  • 17. Using our new object Foo *myObj1=[[Foo alloc] init]; Foo *myObj2=[[Foo alloc] initWithArg:1 andArg:@Pie]; [myObj1 doStuffWithArg:23]; NSLog(@%d and %@,myObj2.anInt, myObj2.aString); //prints out 1 and Pie NSLog(@%d and %@, [myObj2 getAnInt], [myObj2 getAString]); //also prints out 1 and Pie
  • 18. What was that assign/copy stuff? Objective C has a number of ways of doing memory management Garbage collector New and not used everywhere Allocation pools Baby version of garbage collection Reference counting Used everywhere, including with pools and garbage collection
  • 19. What was that assign/copy stuff? When you get an object its reference count is 1 Use retain to add one to the count Use release to drop one from the count When count hits zero the object is destroyed Destructor method is called dealloc
  • 21. What was that assign/copy stuff? In properties you get to say how you want the reference counts done Assign is a simple assignment, no reference counting Retain returns the pointer to an object and ups the reference count by 1 (for non-objects this just works like assign) Copy returns a copy of the object
  • 22. Reference counting This talk is called quick and dirty because thats all the memory management Im mentioning Im still not sure when to retain things as whether things are copied/retained/etc is generally based on function naming conventions. Lame
  • 23. iPhone The iPhone uses Objective C and a bunch of Apple libraries For many apps you can just bolt together library bits with a little bit of extra logic and data
  • 24. The Steve says LETS MAKE AN APP
  • 25. Going Further THE Cocoa programming book seems to be: Cocoa Programming for Mac OS X by Aaron Hillegass
  • 26. Going Further Series of talks by Evan Doll and Alan Cannistraro up on iTunes U Talks done at Stanford University in Summer 2009 for the 2nd year of their iPhone programming course Missing some iPhone OS 3 features, but very good.
  • 27. Going Further Buy a Mac Make The Steve pleased Wear rollneck sweaters Conform Conform Conform Conform Conform Conform Conform