Cs193P - Lecture 3: Iphone Application Development
Cs193P - Lecture 3: Iphone Application Development
Announcements
Assignments 1A and 1B due Thursday 4/9 at 11:59 PM
Enrolled Stanford students can email cs193p@cs.stanford.edu with any questions ! Submit early! Instructions on the website...
!
!
Announcements
Assignments 2A and 2B due Tuesday 4/14 at 11:59 PM
!
Announcements
Troys office hours: Mondays 12-2, Gates B26A Pauls office hours: Tuesdays 12-2, Gates 463 This weeks optional Friday session (4/10)
200-205, 3:15 - 4:05 PM ! Debugging crash course, not to be missed!
!
Todays Topics
Questions from Assignment 1A or 1B? Creating Custom Classes Object Lifecycle Autorelease Objective-C Properties
Custom Classes
Design Phase
Create a class
!
Person NSObject (in this case) Name, age, whether they can vote Cast a ballot
Defining a class
A public header and a private implementation
Header File
Implementation File
Defining a class
A public header and a private implementation
Header File
Implementation File
Defining a class
A public header and a private implementation
Header File
Implementation File
Class Implementation
#import "Person.h" @implementation Person - (int)age { return age; } - (void)setAge:(int)value { age = value; } //... and other methods @end
Superclass methods
As we just saw, objects have an implicit variable named self
!
Object Lifecycle
Object Lifecycle
Creating objects Memory management Destroying objects
Object Creation
Two step process
allocate memory to store the object ! initialize object state
!
+ alloc
!
Class method that knows how much memory is needed Instance method to set initial values, perform other setup
- init
!
} @end
return self;
Less specific ones typically call more specific with default values
- (id)init { return [self initWithName:@No Name]; } - (id)initWithName:(NSString *)name { return [self initWithName:name age:0]; }
Memory Management
Allocation C Objective-C
malloc alloc
Destruction
free dealloc
Otherwise your program may leak or crash One exception, well see in a bit...
Reference Counting
Every object has a retain count
Defined on NSObject ! As long as retain count is > 0, object is alive and valid
!
+alloc and -copy create objects with retain count == 1 -retain increments retain count -release decrements retain count
Balanced Calls
Person *person = nil; person = [[Person alloc] init]; [person setName:@Alan Cannistraro]; [person setAge:29]; [person setWishfulThinking:YES]; [person castBallot]; // When were done with person, release it [person release]; // person will be destroyed here
} @end
Object Ownership
#import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; // Person class owns the name int age; } // method declarations - (NSString *)name; - (void)setName:(NSString *)value; - (int)age; - (void)setAge:(int)age; - (BOOL)canLegallyVote; - (void)castBallot; @end
Object Ownership
#import "Person.h" @implementation Person - (NSString *)name { return name; } - (void)setName:(NSString *)newName { if (name != newName) { [name release]; name = [newName retain]; // names retain count has been bumped up by 1 }
@end
Object Ownership
#import "Person.h" @implementation Person - (NSString *)name { return name; } - (void)setName:(NSString *)newName { if (name != newName) { [name release]; name = [newName copy]; // name has retain count of 1, we own it }
@end
} @end
Autorelease
return result; }
Just right: result is released, but not right away Caller gets valid object and could retain if needed
Autoreleasing Objects
Calling -autorelease flags an object to be sent release at some
point in the future Lets you fulfill your retain/release obligations while allowing an object some additional time to live Makes it much more convenient to manage memory Very useful in methods which return a newly created object
Magic!
(Just kidding...)
When the pool itself is released, it sends -release to all its objects
Pool
Pool created
c un La
p ap h pp A
iti in
zed ali d oa L
ai m
nib n Wa
for it
t en ev
dle an H
t en ev
xit E
pp a
Pool
Pool created
c un La
p ap h pp A
iti in
zed ali d oa L
ai m
nib n Wa
for it
t en ev
dle an H
t en ev
xit E
pp a
Pool
[object autorelease];
Pool created
c un La
p ap h pp A
iti in
zed ali d oa L
ai m
nib n Wa
for it
t en ev
dle an H
t en ev
xit E
pp a
Pool
Pool created
c un La
p ap h pp A
iti in
zed ali d oa L
ai m
nib n Wa
for it
t en ev
dle an H
t en ev
xit E
pp a
Pool
Pool created
c un La
p ap h pp A
iti in
zed ali d oa L
ai m
nib n Wa
for it
t en ev
dle an H
t en ev
xit E
pp a
Pool
Pool released
Pool created
c un La
p ap h pp A
iti in
zed ali d oa L
ai m
nib n Wa
for it
t en ev
dle an H
t en ev
xit E
pp a
Pool
[object release];
Pool created
c un La
p ap h pp A
iti in
zed ali d oa L
ai m
nib n Wa
for it
t en ev
dle an H
t en ev
xit E
pp a
Pool
Pool released
Pool created
c un La
p ap h pp A
iti in
zed ali d oa L
ai m
nib n Wa
for it
t en ev
dle an H
t en ev
xit E
pp a
If you need to hold onto those objects you need to retain them
!
Objective-C Properties
Properties
Provide access to object attributes Shortcut to implementing getter/setter methods Also allow you to specify:
read-only versus read-write access ! memory management policy
!
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; - (BOOL) canLegallyVote; - (void)castBallot; @end
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; - (BOOL) canLegallyVote; - (void)castBallot; @end
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; - (BOOL) canLegallyVote; - (void)castBallot; @end
Defining Properties
#import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } // property declarations @property int age ; @property (copy) NSString * name; @property (readonly) BOOL canLegallyVote ;
- (void)castBallot; @end
Defining Properties
#import <Foundation/Foundation.h> @interface Person : NSObject { // instance variables NSString *name; int age; } // property declarations @property int age; @property (copy) NSString *name; @property (readonly) BOOL canLegallyVote;
- (void)castBallot; @end
Synthesizing Properties
@implementation Person - (int)age { return age; } - (void)setAge:(int)value { age = value; } - (NSString *)name { return name; } - (void)setName:(NSString *)value { if (value != name) { [value release]; name = [value copy]; } } - (void)canLegallyVote { ...
Synthesizing Properties
@implementation Person - (int)age { return age; } - (void)setAge:(int)value { age = value; } - (NSString *)name { return name; } - (void)setName:(NSString *)value { if (value != name) { [value release]; name = [value copy]; } } - (void)canLegallyVote { ...
Synthesizing Properties
@implementation Person - (int)age { age return age; } - (void)setAge:(int)value { age = value; } - (NSString *)name { name return name; } - (void)setName:(NSString *)value { if (value != name) { [value release]; name = [value copy]; } } - (void)canLegallyVote { ...
Synthesizing Properties
@implementation Person @synthesize age; @synthesize name; - (BOOL)canLegallyVote { return (age > 17); } @end
Property Attributes
Read-only versus read-write
! @property int age; // read-write by default ! @property (readonly) BOOL canLegallyVote;
Properties
Mix and match synthesized and implemented properties
@implementation Person @synthesize age; @synthesize name; - (void)setAge:(int)value { age = value; // now do something with the new age value... } @end
Properties In Practice
Newer APIs use @property Older APIs use getter/setter methods Properties used heavily throughout UIKit APIs
!
Not so much with Foundation APIs Properties mean writing less code, but magic can sometimes be non-obvious
Further Reading
Objective-C 2.0 Programming Language
Defining a Class ! Declared Properties
!
Questions?