Download - iPhone Development Crash Course
![Page 1: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/1.jpg)
iPhone Development Crash Course
By Dylan Harris1-11-11
![Page 2: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/2.jpg)
For the tutorials
• xCode working and installed?
![Page 3: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/3.jpg)
Topics today
• iPhone Development Overview• Objective-C Language Basics• Custom classes• Memory management basics• Tutorials throughout
![Page 4: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/4.jpg)
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
![Page 5: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/5.jpg)
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
![Page 6: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/6.jpg)
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
![Page 7: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/7.jpg)
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
![Page 8: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/8.jpg)
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];
![Page 9: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/9.jpg)
Message Syntax
• [receiver message]• [receiver message:argument]• [receiver message:arg1 andArg:arg2]
![Page 10: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/10.jpg)
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
![Page 11: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/11.jpg)
Terminology
• Message expression– [receiver method: argument]
• Message – [receiver method: argument]
• Selector– [receiver method: argument]
• Method – The code selected by a message
![Page 12: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/12.jpg)
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)
![Page 13: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/13.jpg)
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;
![Page 14: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/14.jpg)
Tutorial
• Hello World
• Follow along if xCode and iPhone SDK installed• Use Simulator
![Page 15: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/15.jpg)
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];
![Page 16: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/16.jpg)
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;
![Page 17: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/17.jpg)
Custom classes
• Inherit from NSObject• .h is header• .m is implementation
![Page 18: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/18.jpg)
Header file interface#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;
- (BOOL)canLegallyVote;
@end
![Page 19: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/19.jpg)
Getter/setter methods#import "Person.h”
@implementation Person
- (int)age { return age;} - (void)setAge:(int)value { age = value;}
//... and other methods @end
![Page 20: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/20.jpg)
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];
![Page 21: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/21.jpg)
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];
![Page 22: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/22.jpg)
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];
![Page 23: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/23.jpg)
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
![Page 24: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/24.jpg)
Autorelease
• Example: returning a newly created object-(NSString *)fullName { NSString *result; result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName];
[result autorelease]
return result;}
![Page 25: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/25.jpg)
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!
![Page 26: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/26.jpg)
Properties
• Shortcut for implementing getter/setter methods
![Page 27: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/27.jpg)
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
![Page 28: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/28.jpg)
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
![Page 29: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/29.jpg)
Synthesizing Properties@implementation Person
- (int)age { return age;}
- (void)setAge:(int)value { age = value;}
![Page 30: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/30.jpg)
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
![Page 31: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/31.jpg)
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
![Page 32: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/32.jpg)
Tutorial
• Follow along if xCode and iPhone SDK installed• Use Simulator
![Page 33: iPhone Development Crash Course](https://reader035.vdocument.in/reader035/viewer/2022062322/568147e8550346895db52103/html5/thumbnails/33.jpg)
Comments/Questions?