CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object...
Transcript of CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object...
![Page 1: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/1.jpg)
CS193P - Lecture 3iPhone Application Development
Custom ClassesObject LifecycleAutoreleaseProperties
1Tuesday, January 12, 2010
![Page 2: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/2.jpg)
Announcements• Assignments 1A and 1B due Wednesday 1/13 at 11:59 PM
■ Enrolled Stanford students can email [email protected] with any questions
■ Submit early! Instructions on the website...■ Delete the “build” directory manually, Xcode won’t do it
2Tuesday, January 12, 2010
![Page 3: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/3.jpg)
Announcements• Assignments 2A and 2B due Wednesday 1/20 at 11:59 PM
■ 2A: Continuation of Foundation tool■ Add custom class■ Basic memory management
■ 2B: Beginning of first iPhone application■ Topics to be covered on Thursday, 1/14■ Assignment contains extensive walkthrough
3Tuesday, January 12, 2010
![Page 4: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/4.jpg)
Enrolled students & iTunes U• Lectures have begun showing up on iTunes U• Lead time is longer than last year
• Come to class!!■ Lectures may not post in time for assignments
4Tuesday, January 12, 2010
![Page 5: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/5.jpg)
Office Hours• Paul’s office hours: Thursday 2-4, Gates B26B • David’s office hours: Mondays 4-6pm: Gates 360
5Tuesday, January 12, 2010
![Page 6: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/6.jpg)
Today’s Topics• Questions from Assignment 1A or 1B?• Creating Custom Classes• Object Lifecycle• Autorelease• Objective-C Properties
6Tuesday, January 12, 2010
![Page 7: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/7.jpg)
Custom Classes
7Tuesday, January 12, 2010
![Page 8: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/8.jpg)
Design Phase
8Tuesday, January 12, 2010
![Page 9: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/9.jpg)
Design Phase• Create a class
■ Person
8Tuesday, January 12, 2010
![Page 10: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/10.jpg)
Design Phase• Create a class
■ Person
• Determine the superclass■ NSObject (in this case)
8Tuesday, January 12, 2010
![Page 11: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/11.jpg)
Design Phase• Create a class
■ Person
• Determine the superclass■ NSObject (in this case)
• What properties should it have?■ Name, age, whether they can vote
8Tuesday, January 12, 2010
![Page 12: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/12.jpg)
Design Phase• Create a class
■ Person
• Determine the superclass■ NSObject (in this case)
• What properties should it have?■ Name, age, whether they can vote
• What actions can it perform?■ Cast a ballot
8Tuesday, January 12, 2010
![Page 13: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/13.jpg)
Defining a classA public header and a private implementation
Header File Implementation File
9Tuesday, January 12, 2010
![Page 14: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/14.jpg)
Defining a classA public header and a private implementation
Header File Implementation File
9Tuesday, January 12, 2010
![Page 15: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/15.jpg)
Class interface declared in header file
10Tuesday, January 12, 2010
![Page 16: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/16.jpg)
Class interface declared in header file
@interface Person
@end
10Tuesday, January 12, 2010
![Page 17: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/17.jpg)
Class interface declared in header file
@interface Person
@end
: NSObject
10Tuesday, January 12, 2010
![Page 18: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/18.jpg)
Class interface declared in header file#import <Foundation/Foundation.h>
@interface Person
@end
: NSObject
10Tuesday, January 12, 2010
![Page 19: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/19.jpg)
Class interface declared in header file#import <Foundation/Foundation.h>
@interface Person
@end
{
}
: NSObject
10Tuesday, January 12, 2010
![Page 20: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/20.jpg)
// instance variablesNSString *name;int age;
Class interface declared in header file#import <Foundation/Foundation.h>
@interface Person
@end
{
}
: NSObject
10Tuesday, January 12, 2010
![Page 21: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/21.jpg)
// instance variablesNSString *name;int age;
Class interface declared in header file#import <Foundation/Foundation.h>
@interface Person
@end
{
}
// method declarations- (NSString *)name;- (void)setName:(NSString *)value;
- (int)age;- (void)setAge:(int)age;
- (BOOL)canLegallyVote;- (void)castBallot;
: NSObject
10Tuesday, January 12, 2010
![Page 22: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/22.jpg)
Defining a classA public header and a private implementation
Header File Implementation File
11Tuesday, January 12, 2010
![Page 23: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/23.jpg)
Implementing custom class• Implement setter/getter methods• Implement action methods
12Tuesday, January 12, 2010
![Page 24: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/24.jpg)
Class Implementation
13Tuesday, January 12, 2010
![Page 25: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/25.jpg)
Class Implementation#import "Person.h"
13Tuesday, January 12, 2010
![Page 26: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/26.jpg)
Class Implementation
@implementation Person
@end
#import "Person.h"
13Tuesday, January 12, 2010
![Page 27: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/27.jpg)
Class Implementation
@implementation Person
@end
- (int)age {return age;
}- (void)setAge:(int)value {
age = value;}
//... and other methods
#import "Person.h"
13Tuesday, January 12, 2010
![Page 28: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/28.jpg)
Calling your own methods
14Tuesday, January 12, 2010
![Page 29: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/29.jpg)
Calling your own methods
@implementation Person
@end
- (BOOL)canLegallyVote {
}
- (void)castBallot {
}
#import "Person.h"
14Tuesday, January 12, 2010
![Page 30: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/30.jpg)
Calling your own methods
@implementation Person
@end
- (BOOL)canLegallyVote {
}
- (void)castBallot {
}
#import "Person.h"
return ([self age] >= 18);
14Tuesday, January 12, 2010
![Page 31: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/31.jpg)
Calling your own methods
@implementation Person
@end
- (BOOL)canLegallyVote {
}
- (void)castBallot {
}
#import "Person.h"
if ([self canLegallyVote]) { // do voting stuff} else { NSLog (@“I’m not allowed to vote!”);}
return ([self age] >= 18);
14Tuesday, January 12, 2010
![Page 32: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/32.jpg)
Superclass methods• As we just saw, objects have an implicit variable named “self”
■ Like “this” in Java and C++
• Can also invoke superclass methods using “super”
15Tuesday, January 12, 2010
![Page 33: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/33.jpg)
Superclass methods• As we just saw, objects have an implicit variable named “self”
■ Like “this” in Java and C++
• Can also invoke superclass methods using “super”
- (void)doSomething {// Call superclass implementation first[super doSomething];
// Then do our custom behaviorint foo = bar;// ...
}
15Tuesday, January 12, 2010
![Page 34: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/34.jpg)
Object Lifecycle
16Tuesday, January 12, 2010
![Page 35: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/35.jpg)
Object Lifecycle• Creating objects• Memory management• Destroying objects
17Tuesday, January 12, 2010
![Page 36: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/36.jpg)
Object Creation
18Tuesday, January 12, 2010
![Page 37: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/37.jpg)
Object Creation• Two step process
■ allocate memory to store the object■ initialize object state
18Tuesday, January 12, 2010
![Page 38: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/38.jpg)
Object Creation• Two step process
■ allocate memory to store the object■ initialize object state
+ alloc■ Class method that knows how much memory is needed
18Tuesday, January 12, 2010
![Page 39: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/39.jpg)
Object Creation• Two step process
■ allocate memory to store the object■ initialize object state
+ alloc■ Class method that knows how much memory is needed
- init■ Instance method to set initial values, perform other setup
18Tuesday, January 12, 2010
![Page 40: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/40.jpg)
Create = Allocate + Initialize
19Tuesday, January 12, 2010
![Page 41: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/41.jpg)
Create = Allocate + InitializePerson *person = nil;
19Tuesday, January 12, 2010
![Page 42: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/42.jpg)
Create = Allocate + InitializePerson *person = nil;
person = [[Person alloc] init];
19Tuesday, January 12, 2010
![Page 43: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/43.jpg)
Implementing your own -init method
@implementation Person
@end
#import "Person.h"
20Tuesday, January 12, 2010
![Page 44: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/44.jpg)
Implementing your own -init method
@implementation Person
@end
- (id)init {
}
#import "Person.h"
20Tuesday, January 12, 2010
![Page 45: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/45.jpg)
// allow superclass to initialize its state firstif (self = [super init]) {
}
return self;
Implementing your own -init method
@implementation Person
@end
- (id)init {
}
#import "Person.h"
20Tuesday, January 12, 2010
![Page 46: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/46.jpg)
// allow superclass to initialize its state firstif (self = [super init]) {
}
return self;
Implementing your own -init method
@implementation Person
@end
- (id)init {
}
#import "Person.h"
age = 0;name = @“Bob”;
// do other initialization...
20Tuesday, January 12, 2010
![Page 47: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/47.jpg)
Multiple init methods• Classes may define multiple init methods
- (id)init;- (id)initWithName:(NSString *)name;- (id)initWithName:(NSString *)name age:(int)age;
21Tuesday, January 12, 2010
![Page 48: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/48.jpg)
• 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];}
Multiple init methods• Classes may define multiple init methods
- (id)init;- (id)initWithName:(NSString *)name;- (id)initWithName:(NSString *)name age:(int)age;
21Tuesday, January 12, 2010
![Page 49: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/49.jpg)
Finishing Up With an ObjectPerson *person = nil;
person = [[Person alloc] init];
22Tuesday, January 12, 2010
![Page 50: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/50.jpg)
Finishing Up With an ObjectPerson *person = nil;
person = [[Person alloc] init];
[person setName:@“Jimmy Jones”];[person setAge:32];
[person castBallot];[person doSomethingElse];
22Tuesday, January 12, 2010
![Page 51: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/51.jpg)
Finishing Up With an ObjectPerson *person = nil;
person = [[Person alloc] init];
[person setName:@“Jimmy Jones”];[person setAge:32];
[person castBallot];[person doSomethingElse];
// What do we do with person when we’re done?
22Tuesday, January 12, 2010
![Page 52: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/52.jpg)
Memory Management
Allocation Destruction
C
Objective-C
malloc free
alloc dealloc
23Tuesday, January 12, 2010
![Page 53: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/53.jpg)
Memory Management
Allocation Destruction
C
Objective-C
malloc free
alloc dealloc
23Tuesday, January 12, 2010
![Page 54: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/54.jpg)
Memory Management
Allocation Destruction
C
Objective-C
malloc free
alloc dealloc
23Tuesday, January 12, 2010
![Page 55: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/55.jpg)
Memory Management
Allocation Destruction
C
Objective-C
malloc free
alloc dealloc
• Calls must be balanced■ Otherwise your program may leak or crash
23Tuesday, January 12, 2010
![Page 56: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/56.jpg)
Memory Management
Allocation Destruction
C
Objective-C
malloc free
alloc dealloc
• Calls must be balanced■ Otherwise your program may leak or crash
• However, you’ll never call -dealloc directly■ One exception, we’ll see in a bit...
23Tuesday, January 12, 2010
![Page 57: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/57.jpg)
Reference Counting
24Tuesday, January 12, 2010
![Page 58: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/58.jpg)
Reference Counting• Every object has a retain count
■ Defined on NSObject■ As long as retain count is > 0, object is alive and valid
24Tuesday, January 12, 2010
![Page 59: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/59.jpg)
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
24Tuesday, January 12, 2010
![Page 60: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/60.jpg)
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
24Tuesday, January 12, 2010
![Page 61: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/61.jpg)
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
24Tuesday, January 12, 2010
![Page 62: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/62.jpg)
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
• When retain count reaches 0, object is destroyed• -dealloc method invoked automatically■ One-way street, once you’re in -dealloc there’s no turning back
24Tuesday, January 12, 2010
![Page 63: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/63.jpg)
Balanced CallsPerson *person = nil;
person = [[Person alloc] init];
25Tuesday, January 12, 2010
![Page 64: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/64.jpg)
Balanced CallsPerson *person = nil;
person = [[Person alloc] init];
[person setName:@“Jimmy Jones”];[person setAge:32];
[person castBallot];[person doSomethingElse];
25Tuesday, January 12, 2010
![Page 65: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/65.jpg)
Balanced CallsPerson *person = nil;
person = [[Person alloc] init];
[person setName:@“Jimmy Jones”];[person setAge:32];
[person castBallot];[person doSomethingElse];
// When we’re done with person, release it[person release]; // person will be destroyed here
25Tuesday, January 12, 2010
![Page 66: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/66.jpg)
Reference counting in action
26Tuesday, January 12, 2010
![Page 67: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/67.jpg)
Reference counting in actionPerson *person = [[Person alloc] init];
26Tuesday, January 12, 2010
![Page 68: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/68.jpg)
Reference counting in actionPerson *person = [[Person alloc] init];
Retain count begins at 1 with +alloc
26Tuesday, January 12, 2010
![Page 69: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/69.jpg)
Reference counting in actionPerson *person = [[Person alloc] init];
Retain count begins at 1 with +alloc
[person retain];
26Tuesday, January 12, 2010
![Page 70: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/70.jpg)
Reference counting in actionPerson *person = [[Person alloc] init];
Retain count begins at 1 with +alloc
[person retain];
Retain count increases to 2 with -retain
26Tuesday, January 12, 2010
![Page 71: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/71.jpg)
Reference counting in actionPerson *person = [[Person alloc] init];
Retain count begins at 1 with +alloc
[person retain];
Retain count increases to 2 with -retain
[person release];
26Tuesday, January 12, 2010
![Page 72: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/72.jpg)
Reference counting in actionPerson *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
26Tuesday, January 12, 2010
![Page 73: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/73.jpg)
Reference counting in actionPerson *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];
26Tuesday, January 12, 2010
![Page 74: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/74.jpg)
Reference counting in actionPerson *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
26Tuesday, January 12, 2010
![Page 75: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/75.jpg)
Messaging deallocated objects
27Tuesday, January 12, 2010
![Page 76: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/76.jpg)
Messaging deallocated objectsPerson *person = [[Person alloc] init];// ...[person release]; // Object is deallocated
27Tuesday, January 12, 2010
![Page 77: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/77.jpg)
Messaging deallocated objectsPerson *person = [[Person alloc] init];// ...[person release]; // Object is deallocated
[person doSomething]; // Crash!
27Tuesday, January 12, 2010
![Page 78: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/78.jpg)
Messaging deallocated objectsPerson *person = [[Person alloc] init];// ...[person release]; // Object is deallocated
27Tuesday, January 12, 2010
![Page 79: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/79.jpg)
Messaging deallocated objectsPerson *person = [[Person alloc] init];// ...[person release]; // Object is deallocated
person = nil;
27Tuesday, January 12, 2010
![Page 80: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/80.jpg)
Messaging deallocated objectsPerson *person = [[Person alloc] init];// ...[person release]; // Object is deallocated
person = nil;
[person doSomething]; // No effect
27Tuesday, January 12, 2010
![Page 81: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/81.jpg)
Implementing a -dealloc method
@implementation Person
@end
#import "Person.h"
28Tuesday, January 12, 2010
![Page 82: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/82.jpg)
Implementing a -dealloc method
@implementation Person
@end
- (void)dealloc {
}
#import "Person.h"
28Tuesday, January 12, 2010
![Page 83: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/83.jpg)
Implementing a -dealloc method
@implementation Person
@end
- (void)dealloc {
}
#import "Person.h"
// Do any cleanup that’s necessary// ...
28Tuesday, January 12, 2010
![Page 84: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/84.jpg)
Implementing a -dealloc method
@implementation Person
@end
- (void)dealloc {
}
#import "Person.h"
// Do any cleanup that’s necessary// ...
// when we’re done, call super to clean us up[super dealloc];
28Tuesday, January 12, 2010
![Page 85: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/85.jpg)
Object Lifecycle Recap• Objects begin with a retain count of 1• Increase and decrease with -retain and -release• When retain count reaches 0, object deallocated automatically• You never call dealloc explicitly in your code
■ Exception is calling -[super dealloc]■ You only deal with alloc, copy, retain, release
29Tuesday, January 12, 2010
![Page 86: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/86.jpg)
// instance variablesNSString *name; // Person class “owns” the nameint age;
Object Ownership#import <Foundation/Foundation.h>
@interface Person
@end
{
}
// method declarations- (NSString *)name;- (void)setName:(NSString *)value;
- (int)age;- (void)setAge:(int)age;
- (BOOL)canLegallyVote;- (void)castBallot;
: NSObject
30Tuesday, January 12, 2010
![Page 87: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/87.jpg)
Object Ownership
@implementation Person
@end
#import "Person.h"
31Tuesday, January 12, 2010
![Page 88: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/88.jpg)
Object Ownership
@implementation Person
@end
- (NSString *)name {return name;
}- (void)setName:(NSString *)newName {
}
#import "Person.h"
31Tuesday, January 12, 2010
![Page 89: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/89.jpg)
Object Ownership
@implementation Person
@end
- (NSString *)name {return name;
}- (void)setName:(NSString *)newName {
}
#import "Person.h"
if (name != newName) { [name release]; name = [newName retain];
// name’s retain count has been bumped up by 1}
31Tuesday, January 12, 2010
![Page 90: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/90.jpg)
Object Ownership
@implementation Person
@end
- (NSString *)name {return name;
}- (void)setName:(NSString *)newName {
}
#import "Person.h"
31Tuesday, January 12, 2010
![Page 91: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/91.jpg)
Object Ownership
@implementation Person
@end
- (NSString *)name {return name;
}- (void)setName:(NSString *)newName {
}
#import "Person.h"
if (name != newName) { [name release]; name = [newName copy];
// name has retain count of 1, we own it}
31Tuesday, January 12, 2010
![Page 92: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/92.jpg)
Releasing Instance Variables
@implementation Person
@end
#import "Person.h"
32Tuesday, January 12, 2010
![Page 93: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/93.jpg)
Releasing Instance Variables
@implementation Person
@end
- (void)dealloc {
}
#import "Person.h"
32Tuesday, January 12, 2010
![Page 94: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/94.jpg)
Releasing Instance Variables
@implementation Person
@end
- (void)dealloc {
}
#import "Person.h"
// Do any cleanup that’s necessary[name release];
32Tuesday, January 12, 2010
![Page 95: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/95.jpg)
Releasing Instance Variables
@implementation Person
@end
- (void)dealloc {
}
#import "Person.h"
// Do any cleanup that’s necessary[name release];
// when we’re done, call super to clean us up[super dealloc];
32Tuesday, January 12, 2010
![Page 96: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/96.jpg)
Autorelease
33Tuesday, January 12, 2010
![Page 97: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/97.jpg)
Returning a newly created object- (NSString *)fullName {
NSString *result;
result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName];
return result;}
34Tuesday, January 12, 2010
![Page 98: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/98.jpg)
Returning a newly created object- (NSString *)fullName {
NSString *result;
result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName];
return result;}
Wrong: result is leaked!
34Tuesday, January 12, 2010
![Page 99: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/99.jpg)
Returning a newly created object- (NSString *)fullName {
NSString *result;
result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName];
return result;}
[result release];
34Tuesday, January 12, 2010
![Page 100: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/100.jpg)
Returning a newly created object- (NSString *)fullName {
NSString *result;
result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName];
return result;}
[result release];
Wrong: result is released too early!Method returns bogus value
34Tuesday, January 12, 2010
![Page 101: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/101.jpg)
Returning a newly created object- (NSString *)fullName {
NSString *result;
result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName];
return result;}
34Tuesday, January 12, 2010
![Page 102: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/102.jpg)
Returning a newly created object- (NSString *)fullName {
NSString *result;
result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName];
return result;}
[result autorelease];
34Tuesday, January 12, 2010
![Page 103: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/103.jpg)
Returning a newly created object- (NSString *)fullName {
NSString *result;
result = [[NSString alloc] initWithFormat:@“%@ %@”, firstName, lastName];
return result;}
[result autorelease];
Just right: result is released, but not right awayCaller gets valid object and could retain if needed
34Tuesday, January 12, 2010
![Page 104: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/104.jpg)
Autoreleasing Objects• Calling -autorelease flags an object to be sent release at some
point in the future• Let’s 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
35Tuesday, January 12, 2010
![Page 105: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/105.jpg)
Method Names & Autorelease
36Tuesday, January 12, 2010
![Page 106: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/106.jpg)
Method Names & Autorelease• Methods whose names includes alloc, copy, or new
return a retained object that the caller needs to release
36Tuesday, January 12, 2010
![Page 107: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/107.jpg)
Method Names & Autorelease• Methods whose names includes alloc, copy, or new
return a retained object that the caller needs to releaseNSMutableString *string = [[NSMutableString alloc] init];// We are responsible for calling -release or -autorelease[string autorelease];
36Tuesday, January 12, 2010
![Page 108: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/108.jpg)
Method Names & Autorelease• Methods whose names includes alloc, copy, or new
return a retained object that the caller needs to release
• All other methods return autoreleased objects
NSMutableString *string = [[NSMutableString alloc] init];// We are responsible for calling -release or -autorelease[string autorelease];
36Tuesday, January 12, 2010
![Page 109: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/109.jpg)
Method Names & Autorelease• Methods whose names includes alloc, copy, or new
return a retained object that the caller needs to release
• All other methods return autoreleased objects
NSMutableString *string = [[NSMutableString alloc] init];// We are responsible for calling -release or -autorelease[string autorelease];
NSMutableString *string = [NSMutableString string];// The method name doesn’t indicate that we need to release it// So don’t- we’re cool!
36Tuesday, January 12, 2010
![Page 110: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/110.jpg)
Method Names & Autorelease• Methods whose names includes alloc, copy, or new
return a retained object that the caller needs to release
• All other methods return autoreleased objects
• This is a convention- follow it in methods you define!
NSMutableString *string = [[NSMutableString alloc] init];// We are responsible for calling -release or -autorelease[string autorelease];
NSMutableString *string = [NSMutableString string];// The method name doesn’t indicate that we need to release it// So don’t- we’re cool!
36Tuesday, January 12, 2010
![Page 111: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/111.jpg)
How does -autorelease work?
37Tuesday, January 12, 2010
![Page 112: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/112.jpg)
How does -autorelease work?• Object is added to current autorelease pool
37Tuesday, January 12, 2010
![Page 113: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/113.jpg)
How does -autorelease work?• Object is added to current autorelease pool• Autorelease pools track objects scheduled to be released
■ When the pool itself is released, it sends -release to all its objects
37Tuesday, January 12, 2010
![Page 114: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/114.jpg)
How does -autorelease work?• Object is added to current autorelease pool• Autorelease pools track objects scheduled to be released
■ When the pool itself is released, it sends -release to all its objects
• UIKit automatically wraps a pool around every event dispatch
37Tuesday, January 12, 2010
![Page 115: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/115.jpg)
Autorelease Pools (in pictures)
Launch app
Load main nib
Wait for event
Handle eventExit a
pp
App initialize
d
38Tuesday, January 12, 2010
![Page 116: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/116.jpg)
Pool
Autorelease Pools (in pictures)
Launch app
Load main nib
Wait for event
Handle eventExit a
pp
App initialize
d
Pool created
38Tuesday, January 12, 2010
![Page 117: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/117.jpg)
Pool
Autorelease Pools (in pictures)
Launch app
Load main nib
Wait for event
Handle eventExit a
pp
App initialize
d
Pool created
Objects autoreleased here go into pool
38Tuesday, January 12, 2010
![Page 118: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/118.jpg)
Pool
Autorelease Pools (in pictures)
Launch app
Load main nib
Wait for event
Handle eventExit a
pp
App initialize
d
Pool created
Objects autoreleased here go into pool
38Tuesday, January 12, 2010
![Page 119: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/119.jpg)
Pool
Autorelease Pools (in pictures)
Launch app
Load main nib
Wait for event
Handle eventExit a
pp
App initialize
d
Pool created
Objects autoreleased here go into pool
[object autorelease];
38Tuesday, January 12, 2010
![Page 120: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/120.jpg)
Pool
Autorelease Pools (in pictures)
Launch app
Load main nib
Wait for event
Handle eventExit a
pp
App initialize
d
Pool created
Objects autoreleased here go into pool
38Tuesday, January 12, 2010
![Page 121: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/121.jpg)
Pool
Autorelease Pools (in pictures)
Launch app
Load main nib
Wait for event
Handle eventExit a
pp
App initialize
d
Pool created
Objects autoreleased here go into pool
38Tuesday, January 12, 2010
![Page 122: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/122.jpg)
Pool
Autorelease Pools (in pictures)
Launch app
Load main nib
Wait for event
Handle eventExit a
pp
App initialize
d
Pool released
Pool created
Objects autoreleased here go into pool
38Tuesday, January 12, 2010
![Page 123: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/123.jpg)
Pool[object release];
Autorelease Pools (in pictures)
Launch app
Load main nib
Wait for event
Handle eventExit a
pp
App initialize
d
Pool released
Pool created
Objects autoreleased here go into pool
[object release];
[object release];
38Tuesday, January 12, 2010
![Page 124: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/124.jpg)
Pool
Autorelease Pools (in pictures)
Launch app
Load main nib
Wait for event
Handle eventExit a
pp
App initialize
d
Pool released
Pool created
Objects autoreleased here go into pool
38Tuesday, January 12, 2010
![Page 125: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/125.jpg)
Hanging Onto an Autoreleased Object• Many methods return autoreleased objects
■ Remember the naming conventions...■ They’re hanging out in the pool and will get released later
• If you need to hold onto those objects you need to retain them■ Bumps up the retain count before the release happens
39Tuesday, January 12, 2010
![Page 126: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/126.jpg)
Hanging Onto an Autoreleased Object• Many methods return autoreleased objects
■ Remember the naming conventions...■ They’re hanging out in the pool and will get released later
• If you need to hold onto those objects you need to retain them■ Bumps up the retain count before the release happens
name = [NSMutableString string];
// We want to name to remain valid![name retain];
// ...// Eventually, we’ll release it (maybe in our -dealloc?)[name release];
39Tuesday, January 12, 2010
![Page 127: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/127.jpg)
Side Note: Garbage Collection• Autorelease is not garbage collection• Objective-C on iPhone OS does not have garbage collection
40Tuesday, January 12, 2010
![Page 128: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/128.jpg)
Objective-C Properties
41Tuesday, January 12, 2010
![Page 129: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/129.jpg)
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
42Tuesday, January 12, 2010
![Page 130: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/130.jpg)
#import <Foundation/Foundation.h>
@interface Person : NSObject {
// instance variablesNSString *name;int age;
}
- (void)castBallot;@end
// method declarations- (NSString *) ;- (void)setName:(NSString *)value;- (int) ;- (void)setAge:(int)age;- (BOOL) ;
Defining Properties
name
age
canLegallyVote
43Tuesday, January 12, 2010
![Page 131: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/131.jpg)
#import <Foundation/Foundation.h>
@interface Person : NSObject {
// instance variablesNSString *name;int age;
}
- (void)castBallot;@end
// method declarations- (NSString *) ;- (void)setName:(NSString *)value;- (int) ;- (void)setAge:(int)age;- (BOOL) ;
Defining Properties
name
age
canLegallyVote
43Tuesday, January 12, 2010
![Page 132: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/132.jpg)
#import <Foundation/Foundation.h>
@interface Person : NSObject {
// instance variablesNSString *name;int age;
}
- (void)castBallot;@end
// method declarations- (NSString *) ;- (void)setName:(NSString *)value;- (int) ;- (void)setAge:(int)age;- (BOOL) ;
Defining Properties
name
age
canLegallyVote
43Tuesday, January 12, 2010
![Page 133: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/133.jpg)
#import <Foundation/Foundation.h>
@interface Person : NSObject {
// instance variablesNSString *name;int age;
}
- (void)castBallot;@end
// property declarations@property int ;@property (copy) NSString * ;@property (readonly) BOOL ;
Defining Properties
nameage
canLegallyVote
43Tuesday, January 12, 2010
![Page 134: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/134.jpg)
#import <Foundation/Foundation.h>
@interface Person : NSObject {
// instance variablesNSString *name;int age;
}
- (void)castBallot;@end
// property declarations@property int age;@property (copy) NSString *name;@property (readonly) BOOL canLegallyVote;
Defining Properties
44Tuesday, January 12, 2010
![Page 135: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/135.jpg)
@implementation Person
- (void)canLegallyVote { ...
Synthesizing Properties
- (int)age {return age;
}- (void)setAge:(int)value {
age = value;}- (NSString *)name { return name;}- (void)setName:(NSString *)value { if (value != name) { [name release]; name = [value copy]; }}
45Tuesday, January 12, 2010
![Page 136: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/136.jpg)
@implementation Person
- (void)canLegallyVote { ...
Synthesizing Properties
- (int)age {return age;
}- (void)setAge:(int)value {
age = value;}- (NSString *)name { return name;}- (void)setName:(NSString *)value { if (value != name) { [name release]; name = [value copy]; }}
45Tuesday, January 12, 2010
![Page 137: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/137.jpg)
@implementation Person
- (void)canLegallyVote { ...
Synthesizing Properties
- (int)age {return age;
}- (void)setAge:(int)value {
age = value;}- (NSString *)name { return name;}- (void)setName:(NSString *)value { if (value != name) { [name release]; name = [value copy]; }}
age
name
45Tuesday, January 12, 2010
![Page 138: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/138.jpg)
@implementation Person
Synthesizing Properties
- (BOOL)canLegallyVote {return (age > 17);
}
@end
@synthesize age;@synthesize name;
46Tuesday, January 12, 2010
![Page 139: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/139.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
47Tuesday, January 12, 2010
![Page 140: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/140.jpg)
Property Names vs. Instance Variables• Property name can be different than instance variable
@interface Person : NSObject { int numberOfYearsOld; } @property int age;
@end
48Tuesday, January 12, 2010
![Page 141: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/141.jpg)
Property Names vs. Instance Variables• Property name can be different than instance variable
@interface Person : NSObject { int numberOfYearsOld; } @property int age;
@end
@implementation Person
@synthesize age = numberOfYearsOld;
@end
48Tuesday, January 12, 2010
![Page 142: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/142.jpg)
Properties• Mix and match synthesized and implemented properties
• Setter method explicitly implemented• Getter method still synthesized
@implementation Person @synthesize age;@synthesize name;
- (void)setAge:(int)value { age = value;
// now do something with the new age value...}
@end
49Tuesday, January 12, 2010
![Page 143: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/143.jpg)
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
• You can use either approach■ Properties mean writing less code, but “magic” can sometimes
be non-obvious
50Tuesday, January 12, 2010
![Page 144: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/144.jpg)
Dot Syntax and self• When used in custom methods, be careful with dot syntax for
properties defined in your class• References to properties and ivars behave very differently
@interface Person : NSObject {
NSString *name;}@property (copy) NSString *name;@end
51Tuesday, January 12, 2010
![Page 145: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/145.jpg)
Dot Syntax and self• When used in custom methods, be careful with dot syntax for
properties defined in your class• References to properties and ivars behave very differently
@interface Person : NSObject {
NSString *name;}@property (copy) NSString *name;@end
@implementation Person- (void)doSomething { name = @“Fred”; // accesses ivar directly! self.name = @“Fred”; // calls accessor method}
51Tuesday, January 12, 2010
![Page 146: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/146.jpg)
Common Pitfall with Dot Syntax
@implementation Person- (void)setAge:(int)newAge { self.age = newAge;}@end
What will happen when this code executes?
52Tuesday, January 12, 2010
![Page 147: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/147.jpg)
Common Pitfall with Dot Syntax
@implementation Person- (void)setAge:(int)newAge { self.age = newAge;}@end
@implementation Person- (void)setAge:(int)newAge { [self setAge:newAge]; // Infinite loop!}@end
This is equivalent to:
What will happen when this code executes?
52Tuesday, January 12, 2010
![Page 148: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/148.jpg)
Further Reading• Objective-C 2.0 Programming Language
■ “Defining a Class”■ “Declared Properties”
• Memory Management Programming Guide for Cocoa
53Tuesday, January 12, 2010
![Page 149: CS193P - Lecture 3 · CS193P - Lecture 3 iPhone Application Development Custom Classes Object Lifecycle Autorelease Properties Tuesday, January 12, 2010 1](https://reader034.fdocuments.us/reader034/viewer/2022051905/5ff8125fd446ec04280eefba/html5/thumbnails/149.jpg)
Questions?
54Tuesday, January 12, 2010