Slide 1

Slide 1 text

Cocoa & Objective-C An introduction August 28th, 2008

Slide 2

Slide 2 text

Cocoa & Objective-C • Introduction • Objective-C • Cocoa Touch • iPhone • “CurrencyConverter” Application • Books • References

Slide 3

Slide 3 text

Introduction

Slide 4

Slide 4 text

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

Slide 23

Slide 23 text

Memory Management http://cocoadevcentral.com/d/learn_objectivec/

Slide 24

Slide 24 text

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

Slide 30

Slide 30 text

Accelerometer UIAccelerometer UIAccelerometerDelegate

Slide 31

Slide 31 text

Video MPMoviePlayerController

Slide 32

Slide 32 text

Location Services

Slide 33

Slide 33 text

Graphics & Animation CALayer NSGraphicsContext NSRect / NSMakeRect NSBezierPath NSColor

Slide 34

Slide 34 text

Camera & Photo Library UIImagePickerController UIImagePickerControllerDelegate UIImagePickerControllerSourceTypeCamera UIImagePickerControllerSourceTypePhotoLibrary

Slide 35

Slide 35 text

Code Security

Slide 36

Slide 36 text

More... • Audio • AudioToolbox • AudioUnit • CoreAudio • CoreMIDI • OpenAL • XML • WebKit • SQLite • Networking • CFNetwork • Bonjour

Slide 37

Slide 37 text

CurrencyConverter

Slide 38

Slide 38 text

CurrencyConverter • Simple application for the iPhone • Shows basic workflow • Use of • delegation • Interface Builder / Xcode integration • categories

Slide 39

Slide 39 text

Books

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

References

Slide 45

Slide 45 text

Objective-C http://cocoadevcentral.com/d/learn_objectivec/ http://www.faqs.org/faqs/computer-lang/Objective-C/faq/ http://cs.gmu.edu/~sean/stuff/java-objc.html http://www.mactech.com/articles/mactech/Vol.13/13.03/CandObjectiveCCompared/

Slide 46

Slide 46 text

C Tutorial for Cocoa http://cocoadevcentral.com/articles/000081.php

Slide 47

Slide 47 text

Naming Guidelines http://cocoadevcentral.com/articles/000082.php http://cocoadevcentral.com/articles/000083.php

Slide 48

Slide 48 text

Cocoa Tutorial http://cocoadevcentral.com/d/learn_cocoa/ http://cocoadevcentral.com/d/learn_cocoa_two/

Slide 49

Slide 49 text

Thanks!

Slide 50

Slide 50 text

Questions?