Presentation is loading. Please wait.

Presentation is loading. Please wait.

IPhone Development Crash Course By Dylan Harris 9-19-11.

Similar presentations


Presentation on theme: "IPhone Development Crash Course By Dylan Harris 9-19-11."— Presentation transcript:

1 iPhone Development Crash Course By Dylan Harris 9-19-11

2 For the tutorials Xcode 3 or 4 working and installed? Right now only a few ways to get Xcode 4 – Upgrade to Lion, get of app store for free – Sign up for $100 developer account Maybe it will become free when iOS 5 releases?

3 Topics today iPhone Development Overview Objective-C Language Basics Custom classes Memory management basics Tutorials throughout

4 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(Stanford classes)

5 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 appropriate interface Controller: Middleman between the Model and the View

6 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

7 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 variables declared on heap Loosely typed, if you want

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

9 Message Syntax [receiver message] [receiver message:argument] [receiver message:arg1 andArg:arg2]

10 An example Person *person; //assume exists [person walk]; – (void)walk; int theAge = [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

11 Terminology Message expression – [receiver method: argument] Message – [receiver method: argument] Selector – [receiver method: argument] Method – The code selected by a message

12 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)

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

14 Tutorial Hello World Follow along if Xcode and iPhone SDK installed Use Simulator

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

16 BOOL typedef When ObjC was developed, C had no boolean type (C99 introduced one) ObjC uses 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;

17 Custom classes Inherit from NSObject.h is header.m is implementation

18 Header file interface #import @interface Person : NSObject { // instance variables NSString *name; int age; } // method declarations - (NSString *)name; - (void)setName:(NSString *)value; - (int)age; - (void)setAge:(int)age; - (BOOL)canLegallyVote; @end

19 Getter/setter methods #import "Person.h” @implementation Person -(int)age { return age; } -(void)setAge:(int)value { age = value; } //... and other methods @end

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

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

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

23 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

24 Autorelease Example: returning a newly created object -(NSString *)fullName { NSString *result; result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName]; [result autorelease] return result; }

25 Method Names & Autorelease Methods whose names includes alloc, copy, or new return a retained object that the caller needs to release NSMutableString *string = [[NSMutableString alloc] init]; // We are responsible for calling -release or -autorelease [string autorelease]; All other methods return autoreleased objects 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!

26 Properties Shortcut for implementing getter/setter methods

27 Defining Properties #import @interface Person : NSObject { // instance variables NSString *name; int age; } // method declarations - (NSString *)name; - (void)setName:(NSString *)value; - (int)age; - (void)setAge:(int)age; @end

28 Defining Properties #import @interface Person : NSObject { // instance variables NSString *name; int age; } // method declarations @property (copy) NSString* name; @property int age; @end

29 Synthesizing Properties @implementation Person - (int)age { return age; } - (void)setAge:(int)value { age = value; }

30 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

31 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

32 Tutorial Follow along if Xcode and iPhone SDK installed Use Simulator

33 Comments/Questions?


Download ppt "IPhone Development Crash Course By Dylan Harris 9-19-11."

Similar presentations


Ads by Google