SlideShare una empresa de Scribd logo
1 de 28
A Quick and Dirty Intro to
Objective C and iPhone
Programming
…or how I stopped worrying and learned to love Steve Jobs
What we’re 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’
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 that’s all the memory
management I’m mentioning
• I’m 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

Más contenido relacionado

La actualidad más candente

Slicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPISlicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPIlestrrat
 
FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT ginriki
 
Zpugdccherry 101105081729-phpapp01
Zpugdccherry 101105081729-phpapp01Zpugdccherry 101105081729-phpapp01
Zpugdccherry 101105081729-phpapp01Jeffrey Clark
 
Local search-based pattern matching features in EMF-IncQuery
Local search-based pattern matching features in EMF-IncQueryLocal search-based pattern matching features in EMF-IncQuery
Local search-based pattern matching features in EMF-IncQueryZoltán Ujhelyi
 
Take Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play FrameworkTake Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play FrameworkAsher Glynn
 
Declarative authorization in REST services in SharePoint with F# and ServiceS...
Declarative authorization in REST services in SharePoint with F# and ServiceS...Declarative authorization in REST services in SharePoint with F# and ServiceS...
Declarative authorization in REST services in SharePoint with F# and ServiceS...Sergey Tihon
 

La actualidad más candente (7)

Slicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPISlicing, Dicing, And Linting OpenAPI
Slicing, Dicing, And Linting OpenAPI
 
What's new in C# 8.0 (beta)
What's new in C# 8.0 (beta)What's new in C# 8.0 (beta)
What's new in C# 8.0 (beta)
 
FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT FlawDetector - Rubykaigi2013 LT
FlawDetector - Rubykaigi2013 LT
 
Zpugdccherry 101105081729-phpapp01
Zpugdccherry 101105081729-phpapp01Zpugdccherry 101105081729-phpapp01
Zpugdccherry 101105081729-phpapp01
 
Local search-based pattern matching features in EMF-IncQuery
Local search-based pattern matching features in EMF-IncQueryLocal search-based pattern matching features in EMF-IncQuery
Local search-based pattern matching features in EMF-IncQuery
 
Take Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play FrameworkTake Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play Framework
 
Declarative authorization in REST services in SharePoint with F# and ServiceS...
Declarative authorization in REST services in SharePoint with F# and ServiceS...Declarative authorization in REST services in SharePoint with F# and ServiceS...
Declarative authorization in REST services in SharePoint with F# and ServiceS...
 

Similar a A quick and dirty intro to objective c

iOS Programming - MCV (Delegate/Protocols/Property&Syntax)
iOS Programming - MCV (Delegate/Protocols/Property&Syntax)iOS Programming - MCV (Delegate/Protocols/Property&Syntax)
iOS Programming - MCV (Delegate/Protocols/Property&Syntax)Oliver Lin
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsPetr Dvorak
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective csagaroceanic11
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - CJussi Pohjolainen
 
iOS,From Development to Distribution
iOS,From Development to DistributioniOS,From Development to Distribution
iOS,From Development to DistributionTunvir Rahman Tusher
 
Bootstrapping iPhone Development
Bootstrapping iPhone DevelopmentBootstrapping iPhone Development
Bootstrapping iPhone DevelopmentThoughtWorks
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingsana younas
 
Objective-C talk
Objective-C talkObjective-C talk
Objective-C talkbradringel
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CDataArt
 
Ios fundamentals with ObjectiveC
Ios fundamentals with ObjectiveCIos fundamentals with ObjectiveC
Ios fundamentals with ObjectiveCMadusha Perera
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNico Ludwig
 
Objective-C A Beginner's Dive (with notes)
Objective-C A Beginner's Dive (with notes)Objective-C A Beginner's Dive (with notes)
Objective-C A Beginner's Dive (with notes)Altece
 
Introduction to Python Programing
Introduction to Python ProgramingIntroduction to Python Programing
Introduction to Python Programingsameer patil
 

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

Iphone course 1
Iphone course 1Iphone course 1
Iphone course 1
 
Ios development
Ios developmentIos development
Ios development
 
iOS training (basic)
iOS training (basic)iOS training (basic)
iOS training (basic)
 
iOS Programming - MCV (Delegate/Protocols/Property&Syntax)
iOS Programming - MCV (Delegate/Protocols/Property&Syntax)iOS Programming - MCV (Delegate/Protocols/Property&Syntax)
iOS Programming - MCV (Delegate/Protocols/Property&Syntax)
 
Objective c intro (1)
Objective c intro (1)Objective c intro (1)
Objective c intro (1)
 
FI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS BasicsFI MUNI 2012 - iOS Basics
FI MUNI 2012 - iOS Basics
 
Introduction to objective c
Introduction to objective cIntroduction to objective c
Introduction to objective c
 
Pioc
PiocPioc
Pioc
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - C
 
iOS,From Development to Distribution
iOS,From Development to DistributioniOS,From Development to Distribution
iOS,From Development to Distribution
 
Bootstrapping iPhone Development
Bootstrapping iPhone DevelopmentBootstrapping iPhone Development
Bootstrapping iPhone Development
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
iOS Basic
iOS BasiciOS Basic
iOS Basic
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Objective-C talk
Objective-C talkObjective-C talk
Objective-C talk
 
Никита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-CНикита Корчагин - Programming Apple iOS with Objective-C
Никита Корчагин - Programming Apple iOS with Objective-C
 
Ios fundamentals with ObjectiveC
Ios fundamentals with ObjectiveCIos fundamentals with ObjectiveC
Ios fundamentals with ObjectiveC
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
Objective-C A Beginner's Dive (with notes)
Objective-C A Beginner's Dive (with notes)Objective-C A Beginner's Dive (with notes)
Objective-C A Beginner's Dive (with notes)
 
Introduction to Python Programing
Introduction to Python ProgramingIntroduction to Python Programing
Introduction to Python Programing
 

Último

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 

Último (20)

SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 

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 we’re 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’
  • 20.
  • 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 that’s all the memory management I’m mentioning • I’m 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