I want to understand how to set the parameters of properties (accessors).
I took the following code from an example of Kal calendar.
// Holiday.h
@interface Holiday : NSObject
{
NSDate *date;
NSString *name;
NSString *country;
}
@property (nonatomic, retain, readonly) NSDate *date;
@property (nonatomic, retain, readonly) NSString *name;
@property (nonatomic, retain, readonly) NSString *country;
- (id)initWithName:(NSString *)name country:(NSString *)country date:(NSDate *)date;
@end
// Holiday.m
#import "Holiday.h"
@implementation Holiday
@synthesize date, name, country;
- (id)initWithName:(NSString *)aName country:(NSString *)aCountry date:(NSDate *)aDate
{
if ((self = [super init])) {
name = [aName copy];
country = [aCountry copy];
date = [aDate retain];
}
return self;
}
- (void)dealloc
{
[date release];
[name release];
[country release];
[super dealloc];
}
@end
1) The properties are set to retain, but since the setter can't be used the retain makes no sense here.
2) In addition, in the initWithName method the values are set with copy. Why not directly defining the properties with copy and using the accessor methods?
@property (nonatomic, copy) NSString *name;
// ...
self.name = aName;
3) Do I need the readonly here? I don't know why they are used here. If I would use copy together with the setter the readonly prohibits me to set the value, because there is no setter.
4) In the initWithName method sometimes copy and sometimes retain is used. I would suggest to use always copy here, because the value should not get modified later.
5) What I can remember is that it is OK to copy/retain in the initWithName and release in the dealloc method.
So how would you suggest to use retain, copy and readonly in this example here?