0

Are my two assumptions about properties correct?

@interface Foo : NSObject {
NSDate *Created;
}

@property (nonatomic, retain) NSDate *Created;

@end

@implementation Foo

@synthesize Created;

- (id) init {
if(self = [super init])
{
    Created = [NSDate date]; //this will not call the setter and instead just access the variable directly, which means it will not automatically get retained for me.
    self.Created = [NSDate date]; // this will call the setter, which will retain the variable automatically for me.
}
return self;
}

- (void)dealloc {
    [Created release]   
    [super dealloc];
}
@end
odyth
  • 4,324
  • 3
  • 37
  • 45

2 Answers2

7

Yes; that is correct.

Note that the instance variable should be created; it should start with a lowercase letter. I'd also recommend creationDate.

jlehr
  • 15,557
  • 5
  • 43
  • 45
bbum
  • 162,346
  • 23
  • 271
  • 359
0

It is recommended not to use properties in dealloc or init, so in the init method instead of doing self.propertyName = [NSDate date]; you should do fieldName = [[NSDate alloc] init]; the release in dealloc is fine though

More about it

Community
  • 1
  • 1
Zaky German
  • 14,324
  • 4
  • 25
  • 31
  • I don't buy the "don't use properties in init" stuff. I use "retain" properties specifically so I don't have to pay such close attention to retains/releases. Whenever I try to "optimize" by using raw ivar references I invariably end up with a retain/release bug. I don't use custom setters/getters, just the default @synthesize, which is perfectly fine for use in init. – Bogatyr Feb 21 '11 at 21:41
  • 1
    When the only places you use the ivar references are init and dealloc it's not that easy to screw something up. I don't use it for optimization but simply because that's what apple recommends. – Zaky German Feb 21 '11 at 23:37