Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cocoa & Objective-C: An Introduction

Cocoa & Objective-C: An Introduction

Introduction to Cocoa and Objective-C given to my colleagues of Electronlibre on August 28th, 2008.

Adrian Kosmaczewski

August 28, 2008
Tweet

More Decks by Adrian Kosmaczewski

Other Decks in Technology

Transcript

  1. Cocoa & Objective-C • Introduction • Objective-C • Cocoa Touch

    • iPhone • “CurrencyConverter” Application • Books • References
  2. 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
  3. 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
  4. 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!
  5. Calling Methods • “Message Passing” ≠ “Method Call” [object method];

    [object methodWithParam:parameter and:other]; • Interface and implementation are decoupled
  6. The “id” type • Placeholder to any type: id name

    = @”Adrian”; • Similar to NSString* name = @”Adrian”; • In the latter form, we get compiler checks
  7. Creating Objects • No “new” operator; static and instance methods

    used instead: MyClass *value = nil; value = [[MyClass alloc] init];
  8. Creating Objects // C++ // Memory freed when out of

    scope std::string name(“Adrian”); std::string *name = NULL; name = new std::string(“Adrian”); delete name;
  9. Memory Management • Cocoa allows Garbage Collection only for desktop

    apps. • iPhone applications DO NOT use Garbage collection, but manual memory management.
  10. Memory Management • Objects created using “alloc” have a “retain

    count” of “1”: // increments retain count [object retain]; // decrements retain count [object release];
  11. 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”
  12. Cocoa Touch • Reduced version of the Cocoa framework found

    in Mac OS X Leopard • Divided in two parts: • UIKit • Foundation
  13. More... • Audio • AudioToolbox • AudioUnit • CoreAudio •

    CoreMIDI • OpenAL • XML • WebKit • SQLite • Networking • CFNetwork • Bonjour
  14. CurrencyConverter • Simple application for the iPhone • Shows basic

    workflow • Use of • delegation • Interface Builder / Xcode integration • categories