IPhone Tech Talks/Introduction to Objective-C and Cocoa Touch


 * Matt Drance (App Framework Evangelist)
 * Michael Jurewitz (Development Tools and Performance Evangelist)

iPhone OS

 * Code Signing
 * Single app/process environment

Sandboxing

 * frameworks allow access to some non-sandboxed data

Memory management

 * no swap space
 * issues memory warning prompting cleanup
 * quit app if exceed

Objective C
C language with Smalltalk-style syntax --> ObjC

C++:


 * fast but static
 * compile-time decisions, little runtime flexibility

Objective-C:


 * dynamic
 * runtime decision making
 * runtime class extensions

Objective-C
can be nested!

Messaging

 * - class, method
 * - instance of class, method with parameter/argument
 * - instance, multiple parameters
 * you know what argument type fits which position, self-documenting

Selectors

 * is 'newEmployee'
 * is 'setRaise:'
 * is 'setWork:withDeadline'

Method dispatch

 * class hierarchy (single only, not multiple) - Employee -> Person -> NSObject
 * NSObject is the root object, defined in Foundation framework
 * Employee gets chance to respond to message, then Person, then NSObject
 * 'firstName' method
 * in this example, Employee has no firstName method, but Person has a firstName method, so it gets it
 * 'description' method
 * defined on NSObject
 * very basic way to print itself out at commandline / memory
 * can override method for debug - output state, etc.
 * what happens when no response to message?
 * traverses classes again for opportunity to pass to helper object
 * exception raised if no helper object to forward to
 * exceptions raised only in 'exceptional circumstances' (cute)

Initialising instance
super = send messages to superclass

Memory management

 * runtime tracks object's retain count for entire lifetime of object (starts at 1 on creation)
 * need to stick around? send it the retain message
 * done with it? release it with -release
 * once no longer needed by any part of system, cleared out of memory!

- retain count 1 - retain 2 - retain 1 - retain 0, cleared from memory

Autorelease pools

 * use  message
 * sends  at some point in future (delayed)
 * used in class convenience methods for temporary objects


 * if  or , must
 * if used convenience method, must  to keep

Example of memory management
comparison of objects

if newName != name, retain newName, release name, and name=newName (returned)

dealloc function - lets you release stuff within class

Setter/Getter Pattern

 * simple but repetitive
 * must create code for each getter/setter

Properties

 * quickly and easily create set/get pairs
 * specify storage semantics
 * easy to override

- attributes, type, name

readwrite is default for @property
 * attributes
 * readonly/readwrite (mutability)
 * assign/retain/copy (memory management)
 * nonatomic (concurrency)
 * setter= / getter= (API for setter/getter names, ex: bool getter -> isGoingFast)

can create custom/overridden methods

Objective-C Categories

 * mix in custom behaviour
 * seamlessly add methods to existing classes
 * should use subclass instead to add storage or modify behaviour

uses, could, for example, add a method to NSString

Objective-C Protocols

 * multiple inheritance via specifications
 * way for objects to communicate between each other, identical to Java interfaces
 * list of methods, classes can opt-in to conform to protocol by implementing methods and advertising that they comply

uses

used in UITableView for datasourcing

Cocoa Touch

 * two pieces of framework
 * UIKit - event handling, graphics and windowing, text and web management
 * Foundation - object wrappers, strings, data structures, networking and other system services

Strings

 * supports sprintf formatting
 * convenience methods in NSString and most of Foundation API - single line stuff like pathComponents (creates array out of /path/to/file), lastPathComponent, stringWithContentsOfFile, etc.
 * if you spend 30 lines of code doing something simple, probably missed something!

Immutable arrays

 * convenience methods, like componentsJoinedByString

Dictionaries (aka hashtables in other frameworks)

 * also come in immutable/mutables
 * array with keys, essentially

File I/O

 * NSFileManager class handles lots of this stuff
 * tmp directory created in root of sandbox, periodically cleared by iPhone
 * Library/Caches - saved, not backed up (speeds up sync!!!) - use for non-critical data you don't want to re-download each time
 * EVERYTHING ELSE IS BACKED UP

NSLog

 * outputs debug information to debugger console
 * supports printf formatting
 * also accepts object arguments easily -  (uses description method for pretty format)

Bundles

 * special type of folder
 * your application is an example of one
 * executables, nib files (user interface), metadata
 * accessible via  class, gives access to static resources (default data, images, sounds)
 * application bundle is READ ONLY, new data goes in the sandbox instead
 * example: pull a default sqlite database if no user data

Networking

 * standard sockets programming
 * ,  (CF = Core Foundation)
 * POSIX APIs as well
 * for async requests
 * - xml parser
 * Bonjour networking (zero config discovery of peers on same network)

UIKit

 * visual components
 * can have third-party ones
 * Image views - scaling! resize, aspect ratio, centring, etc.
 * Web views

MVC

 * Model - Foundation API
 * View - UIKit Views
 * Controller - UIKit View Controllers

Target-action

 * Control has a target and an action
 * Target -> Object
 * Action -> Selector
 * ex. tap button, sends action to target

Notifications

 * Notification centre - looks for observers to send to
 * Observer / controller
 * movie player, as example, has 'finished loading', 'finished playing' notifications

Delegation

 * default image is displayed
 * instantiate
 * load main nib
 * load main window
 * create instance of application delegate
 * UI config
 * first and last chance to act
 * custom class written by you
 * conforms to
 * hooked up in interface builder


 * two key methods to delegation
 * - load initial UI, saved state
 * - save state and data
 * just a courtesy - DO NOT ASSUME YOU HAVE A LOT OF TIME! quick and easy stuff, important data should be at time of user-entry