hey guys question:
i was doing an exercise and the book said to write the following snippet
and said we need to retain the object name (defined in my class definition). my question is why do we need to retain this object? i mean ... in my main functions i have
to make this more clear here is my class interface
So two questions
1. which calls initwithcoder, and name and grade are placed into the new object from my class. so why bother retaining it? i understand that decodeobject for key uses autorelease itself, so wouldn't that "autorelease" kick in after i do [fred release] in my main?
2. WHY use retain in @property (retain) NSString *name;??? i believe @property (matched with synthesize) must makes setters and getters.. so why use retain?
thanks
i was doing an exercise and the book said to write the following snippet
Code:
-(id) initWithCoder:(NSCoder *) coder {
name = [[coder decodeObjectForKey:@"EMPname"] retain];
NSNumber *gradeBox = [coder decodeObjectForKey:@"EMPgrade"];
grade = (int) [gradeBox integerValue];
return self;
}
and said we need to retain the object name (defined in my class definition). my question is why do we need to retain this object? i mean ... in my main functions i have
Code:
Employee *fred = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/YOURDIRECTORY/bob.plist"];
to make this more clear here is my class interface
Code:
@interface Employee : NSObject {
@private
NSString *name;
int grade;
}
@property (retain) NSString *name;
@property int grade;
@end
So two questions
1. which calls initwithcoder, and name and grade are placed into the new object from my class. so why bother retaining it? i understand that decodeobject for key uses autorelease itself, so wouldn't that "autorelease" kick in after i do [fred release] in my main?
2. WHY use retain in @property (retain) NSString *name;??? i believe @property (matched with synthesize) must makes setters and getters.. so why use retain?
thanks
Last edited: