@synthesize or not @synthesize


#1

Hi all,
So far I was declaring all my instance variables as properties in header file and instead of synthesizing them in implementation file I was using self. But with latest example there is an issue. If I declare dateCreated property as (readonly) I cannot do self.dateCreated = [[NSDate alloc] init]; since Xcode gives an error - Assignment to readonly property. However if I synthesize dateCreated and use dateCreated = [[NSDate alloc] init]; everything works fine.
I thought that we don’ t need to synthesize properties/instance variables, it’s done automatically behind the scenes.
What’s the difference between self.dateCreated and dateCreated in this case? Why assignment/object initialization being treated as setting property value?
Thank you very much.


#2

See Encapsulating Data in Programming with Objective-C. You can find the answers there.


#3

ibex10, thank you for pointing in the right direction (didn’t expect you to make my life any easier:))

Am I right to assume that dateCreated is an ivar and self.dateCreated is a property? And readonly works only for properties through accessor methods and if we access an ivar directly we can ditch readonly attribute? If Xcode treats any assignment as a setter we (having set readonly attribute) can no longer instantiate an object?

[code]-(id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
{
//Call the superclass’s designated initializer
self = [super init];

//Did the superclass's designated initializer succeed?
if (self) {
    
    //Give the instance variables initial values
    [self setItemName:name];
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    [color=#FF0000]self.dateCreated = [[NSDate alloc] init];[/color]
}

//Return the address of the newly initialized object
return self;

}[/code]

P.S. Techrepublic has a good article on properties and ivars. Not sure if I can post links here.


#4

Ok, there are two ways of setting readonly property. With @synthesize and using ivar like dateCreated and without @synthesize accessing ivar with underscore _dateCreated. Or by redeclaring the property as readwrite in your private @interface
The only question remains is why we had this example in the book (instantiating readonly property)?
Just to be clear - books are awesome and I love learning this way - trial and error.


#5

So example init method should be:

[code]-(id)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
{
//Call the superclass’s designated initializer
self = [super init];

//Did the superclass's designated initializer succeed?
if (self) {
    
    //Give the instance variables initial values
    _itemName = name;
    _serialNumber = sNumber;
    _valueInDollars = value;
    _dateCreated = [[NSDate alloc] init];
}

//Return the address of the newly initialized object
return self;

}[/code]

Per Apple’s: “Setter methods can have additional side-effects. They may trigger KVC notifications, or perform further tasks if you write your own custom methods. You should always access the instance variables directly from within an initialization method because at the time a property is set, the rest of the object may not yet be completely initialized.”

P.S. This applies to auto-synthesized properties.