330 likes | 470 Views
iPhone Development Crash Course. By Dylan Harris 1-11-11. For the tutorials. xCode working and installed?. Topics today. iPhone Development Overview Objective-C Language Basics Custom classes Memory management basics Tutorials throughout. Getting help. API & Conceptual Docs
E N D
iPhone Development Crash Course By Dylan Harris 1-11-11
For the tutorials • xCode working and installed?
Topics today • iPhone Development Overview • Objective-C Language Basics • Custom classes • Memory management basics • Tutorials throughout
Getting help • API & Conceptual Docs • Found in xCode under help menu • Also found on developer.apple.com • Class header files • The Internet • Stack Overflow, blogs, iTunes U
Model-View-Controller • iPhone dev uses the MVC design pattern • Model: Manages the app state and data • View: Presents the Model to the user in an approriate interface • Controller: Middleman between the Model and the View
Cocoa Frameworks • Foundation • Check out object docs • NSString • Objective-C string constant: @”This is a string” • NSArray, NSDictionary, NSSet • Their “mutable” counterparts • UIKit • User interface elements
Objective-C • Strict superset of C • Mix C with ObjC • Single inheritance • Classes inherit from one and only one superclass • Protocols define behavior that cross classes • Dynamic runtime • All vars declared on heap • Loosely typed, if you want
Syntax Additions • New types • id: anonymous object, allows for loose typing • Class: a class is also an object • Selector: like a function pointer • Syntax for message expressions • [receiver message:argument];
Message Syntax • [receiver message] • [receiver message:argument] • [receiver message:arg1 andArg:arg2]
An example • Person *person; //assume exists • [person walk]; • (void)walk; • inttheAge = [person age]; • (int)age; • [person setAge:25 andHeight:60]; • (void)setAge:(int)age andHeight:(int)height; • NSString* spouseName = [[person spouse] name]; • (Person*)spouse • (NSString*)name
Terminology • Message expression • [receiver method: argument] • Message • [receiver method: argument] • Selector • [receiver method:argument] • Method • The code selected by a message
Classes and Instances • Both classes and instances are objects • Classes are messaged to create instances • Instances respond to instance methods • (id)init; • (void)walk; • Classes respond to class methods • (id)alloc; • (Person*)sharedPerson; • You can ask an object about it’s class(introspection)
Dot Syntax • Convenient shorthand for invoking accessor methods • float height = [person height]; • float height = person.height; • [person setHeight:newHeight]; • person.height= newHeight; • Follows the dots... • [[person child] setHeight:newHeight]; • // exactly the same as person.child.height = newHeight;
Tutorial • Hello World • Follow along if xCode and iPhone SDK installed • Use Simulator
Null Object Pointer • Test for nil explicitly • if (person == nil) return; • Or implicitly • if (!person) return; • Can use in assignments and as arguments if expected • person = nil; • [button setTarget: nil]; • Sending a message to nil? Perfectly fine • person = nil; • [person walk];
BOOL typedef • When ObjC was developed, C had no boolean type (C99 introduced one) • ObjCuses a typedef to define BOOL as a type • BOOL flag = NO; • Macros included for initialization and comparison: YES and NO (TRUE and FALSE are also typedef) • if (flag == YES) • if (flag) • if (!flag) • if (flag != YES) • flag = YES; • flag = 1;
Custom classes • Inherit from NSObject • .h is header • .m is implementation
Header file interface #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString*name; intage; } // method declarations - (NSString *)name; - (void)setName:(NSString *)value; - (int)age; - (void)setAge:(int)age; - (BOOL)canLegallyVote; @end
Getter/setter methods #import "Person.h” @implementation Person • (int)age { return age; } • (void)setAge:(int)value { age = value; } //... and other methods @end
Action methods #import "Person.h” @implementation Person • (BOOL)canLegallyVote{ return ([self age] >= 18); } @end • Note the “self” object, same as this pointer in C++ • Can also call “super” to access methods in the superclass • [super dosomething];
Object creation • Two step process • Allocate memory to store the object • Initialize the object state • [[Person alloc] init] returns a new Person • Can have other init methods • [[Person alloc] initWithAge:25];
Reference Counting • Every NSObject has a retain count • +alloc and -copy create objects with retain count of 1 • -retain increments retain count • -release decrements retain count • Retain count reaches 0, -dealloc automatically called and object is destroyed Person *person = [[Person alloc] init]; [person doSomething]; [person release];
Reference counting in action Person *person = [[Person alloc] init]; Retain count begins at 1 with +alloc [person retain]; Retain count increases to 2 with -retain [person release]; Retain count decreases to 1 with -release [person release]; Retain count decreases to 0, -dealloc automatically called
Autorelease • Example: returning a newly created object -(NSString *)fullName { NSString*result; result = [[NSStringalloc] initWithFormat:@“%@ %@”, firstName, lastName]; [result autorelease] return result; }
Method Names & Autorelease • Methods whose names includes alloc, copy, or new return a retained object that the caller needs to release NSMutableString *string = [[NSMutableStringalloc] init]; // We are responsible for calling -release or -autorelease [string autorelease]; • All other methods return autoreleasedobjects NSMutableString *string = [NSMutableString string]; // The method name doesn’t indicate that we need to release it, so don’t • This is a convention- follow it in methods you define!
Properties • Shortcut for implementing getter/setter methods
Defining Properties #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } // method declarations - (NSString *)name; - (void)setName:(NSString *)value; - (int)age; - (void)setAge:(int)age; @end
Defining Properties #import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } // method declarations @property (copy) NSString* name; @property int age; @end
Synthesizing Properties @implementation Person - (int)age { return age; } - (void)setAge:(int)value { age = value; }
Synthesizing Properties @implementation Person @synthesize age; - (void)setAge:(int)value { age = value; // now do something with the new age value... } • Can mix and match synthesized and implemented properties • Synthesize age, but implement setAge • Getter method still synthesized
Property Attributes • Read-only versus read-write @property int age; // read-write by default @property (readonly)BOOL canLegallyVote; • Memory management policies (only for object properties) @property (assign)NSString *name; // pointer assignment @property (retain)NSString *name; // retain called @property (copy)NSString *name; // copy called
Tutorial • Follow along if xCode and iPhone SDK installed • Use Simulator