History
• Objective-C
• The “other” OO language based in C
• Created by Brad Cox (~1980)
• Smalltalk syntax around C
• NeXT
• The “other” company created by Steve Jobs
• NeXTstep: the father of Cocoa
Slide 5
Slide 5 text
No content
Slide 6
Slide 6 text
No content
Slide 7
Slide 7 text
Legacy
Slide 8
Slide 8 text
No content
Slide 9
Slide 9 text
No content
Slide 10
Slide 10 text
Objective-C
Slide 11
Slide 11 text
The Language
• Thin layer around C
• Message-dispatch runtime
• Static and dynamic (you choose)
• The “real” father of Java:
• http://cs.gmu.edu/~sean/stuff/java-objc.html
Slide 12
Slide 12 text
Comparison
Objective-C Java
@interface & @implementation class
@protocol interface
#import // files! import // classes!
categories n/a
id n/a
Slide 13
Slide 13 text
Characteristics
• Single inheritance + interfaces (“@protocols”)
• @protocols can have optional methods
• Fields protected by default
• Methods can be added to existing classes
• Full introspection / reflection
• Messages can be intercepted and forwarded
• à la AOP!
Slide 14
Slide 14 text
Classes
Slide 15
Slide 15 text
Methods
Slide 16
Slide 16 text
Calling Methods
• “Message Passing” ≠ “Method Call”
[object method];
[object methodWithParam:parameter
and:other];
• Interface and implementation are decoupled
Slide 17
Slide 17 text
The “id” type
• Placeholder to any type:
id name = @”Adrian”;
• Similar to
NSString* name = @”Adrian”;
• In the latter form, we get compiler checks
Slide 18
Slide 18 text
Creating Objects
• No “new” operator; static and instance methods used instead:
MyClass *value = nil;
value = [[MyClass alloc] init];
Slide 19
Slide 19 text
Creating Objects
• Cannot create objects on the stack:
• All objects are created on the heap!
Slide 20
Slide 20 text
Creating Objects
// C++
// Memory freed when out of scope
std::string name(“Adrian”);
std::string *name = NULL;
name = new std::string(“Adrian”);
delete name;
Slide 21
Slide 21 text
Memory Management
• Cocoa allows Garbage Collection only for desktop apps.
• iPhone applications DO NOT use Garbage collection, but manual
memory management.
Slide 22
Slide 22 text
Memory Management
• Objects created using “alloc” have a “retain count” of “1”:
// increments retain count
[object retain];
// decrements retain count
[object release];
Memory Management
• Only one rule:
• “If you create an object with alloc or copy, send it a
release message at the end of the function. If you create an
object any other way, do nothing”
Slide 25
Slide 25 text
Cocoa Touch
Slide 26
Slide 26 text
Cocoa Touch
• Reduced version of the Cocoa framework found in Mac OS X
Leopard
• Divided in two parts:
• UIKit
• Foundation
Slide 27
Slide 27 text
UIKit
Slide 28
Slide 28 text
iPhone OS
Slide 29
Slide 29 text
Address Book
ABAddressBook
ABMultiValue
ABMutableMultiValue
ABRecord
ABGroup
ABPerson