1 / 33

iPhone Development Crash Course

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

liona
Download Presentation

iPhone Development Crash Course

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. iPhone Development Crash Course By Dylan Harris 1-11-11

  2. For the tutorials • xCode working and installed?

  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

  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 approriate 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 vars 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; • 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

  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) • 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;

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

  18. 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

  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 = [[NSStringalloc] 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 = [[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!

  26. Properties • Shortcut for implementing getter/setter methods

  27. 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

  28. 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

  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?

More Related