Objective-C Primer iphone Programmer s Association Lorenzo Swank September 10, 2008
Disclaimer Content was blatantly and unapologetically stolen from the WWDC 2007 Fundamentals of Cocoa session, as well as from CocoaDevCentral.com. This content, the ipa modifications to it, and my sarcastic comments are not endorsed or approved by the original content authors. All material is used for academic purposes and is, therefore, probably fair use. Thank you.
Overview What is Cocoa Touch? Objective-C Overview
Resources Apple Developer Connection Resource Library http://developer.apple.com/resourcelibrary
What is Cocoa Touch? Object-oriented APIs Frameworks Foundation UIKit Others
Foundation Core framework for non-ui functionality - Root class
Foundation Core framework for non-ui functionality - Root class NSObject
Foundation Core framework for non-ui functionality - Root class - Operating system facilities and interaction NSFileManager, NSBundle, NSPort, NSStream, NSTask, NSHost, NSRunLoop, NSDate
Foundation Core framework for non-ui functionality - Root class - Operating system facilities and interaction - Internationalization NSString, NSCharacterSet, NSNumberFormatter, NSDateFormatter, NSLocale
Foundation Core framework for non-ui functionality - Root class - Operating system facilities and interaction - Internationalization - Collections NSArray, NSMutableArray, NSDictionary, NSSet, Property Lists
Foundation Core framework for non-ui functionality - Root class - Operating system facilities and interaction - Internationalization - Collections - Scripting NSAppleScript, NSScriptCommand
Foundation Core framework for non-ui functionality - Root class - Operating system facilities and interaction - Internationalization - Collections - Scripting - XML processing, Web access NSXMLParser, NSXMLDocument, NSXMLNode, NSURL, NSUrlRequest, NSUrlResponse
Foundation Core framework for non-ui functionality - Root class - Operating system facilities and interaction - Internationalization - Collections - Scripting - XML processing, Web access - Lots of other stuff
UIKit Core framework for UI functionality - Top Level Interfaces UIApplication, UIScreen
UIKit Core framework for UI functionality - Top Level Interfaces - Drawing UIWindow, UIView, UIColor, UIFont, UIImage, UIImageView
UIKit Core framework for UI functionality - Top Level Interfaces - Drawing - Event Handling UIEvent, UIAcceleration, UIAccelerometer, UITouch
UIKit Core framework for UI functionality - Top Level Interfaces - Drawing - Event Handling - Standard Controls UIControl, UIButton UILabel, UISegmentedControl, UIActivityIndicatorView, UIDatePicker
UIKit Core framework for UI functionality - Top Level Interfaces - Drawing - Event Handling - Standard Controls - Standard Views UIScrollView, UIWebView, UITableView
UIKit Core framework for UI functionality - Top Level Interfaces - Drawing - Event Handling - Standard Controls - Standard Views - Standard View Controllers UIViewController, UINavigationController, UITabBarController, UITableViewController, UIImagePickerController
Interface Builder
UICatalog
What is Objective-C? Small superset of C - ANSI C - Some additional syntax - A few additional types Dynamic object runtime Single inheritance
Objective-C Defining a Method -(void)setwidth:(int)width height:(int)height { _width = width; _height = height; } Sending a message (Calling a method) [contentview setwidth:100 height:100]; [[viewarrays objectatindex:n] setwidth:100
Objective-C C Style: Method Names CreateAlertView(0.0f, 0.0f, 200.0f, 150.0f, warnstring, TRUE); Objective-C Style [[AlertView alloc] initwithoriginx: 0.0f originy: 0.0f width: 200.0f height: 150.0f text: warningstring, fadeoutwhenfinished: TRUE];
Objective-C Referring to a Method SEL myselector = @selector(setwidth:height:); Referring to an object Widget* currwidget; id currwidget; Referring to self [self setwidth:150 height:150];
Objective-C String literals NSString* stringname = @ This is a little text ;
Memory Management Cocoa Touch objects are reference counted
Memory Management Cocoa Touch objects are reference counted A freshly created object has a reference count of one Widget* mywidget = [[Widget alloc] init]; Widget* yourwidget = [mywidget copy];
Memory Management Cocoa Touch objects are reference counted A freshly created object has a reference count of one Widget* mywidget = [[Widget alloc] init]; Widget* yourwidget = [mywidget copy]; retain increments the reference count
Memory Management Cocoa Touch objects are reference counted A freshly created object has a reference count of one Widget* mywidget = [[Widget alloc] init]; Widget* yourwidget = [mywidget copy]; retain increments the reference count release decrements the reference count
Memory Management Cocoa Touch objects are reference counted A freshly created object has a reference count of one Widget* mywidget = [[Widget alloc] init]; Widget* yourwidget = [mywidget copy]; retain increments the reference count release decrements the reference count An instance of the object is deallocated when its reference count reaches 0 - Object is sent dealloc
Memory Management Cocoa Touch objects are reference counted A freshly created object has a reference count of one Widget* mywidget = [[Widget alloc] init]; Widget* yourwidget = [mywidget copy]; retain increments the reference count release decrements the reference count An instance of the object is deallocated when its reference count reaches 0 - Object is sent dealloc autorelease decrements a reference count later
Memory Management
Memory Management
Reference Counting alloc [NSObjectInstance alloc] +1 copy [NSObjectInstance copy] +1 retain [NSObjectInstance retain] +1 release [NSObjectInstance release] -1 autorelease [NSObjectInstance autorelease] -1*
Object Ownership If you create it, you own it. alloc/init, copy If you were handed an object you re going to need, retain it. When you pass an object to someone else don t retain it for them.
Important Concerns What happens...... if you release an object before you are done with it?... if an object is deallocated before you are done with it?... if you explicitly call dealloc?... if you don t retain an object that you need?
Summary Cocoa Touch Frameworks Foundation UIKit Objective-C Syntax Memory Management
End
ipa Areas of Research Call for Volunteers iphone Hardware Interfacing with other devices UIKit Making it do things that are not allowed