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
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];
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
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
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