Question

I use setters quite often. This is the code I have for this one:

.h:

@property (nonatomic, strong) NSDate *lastSyncDate;

.m:

-(void)setLastSyncDate:(NSDate *)lastSyncDate {
    _lastSyncDate = lastSyncDate;

    //Do something
}

Then I put in a getter method:

-(NSDate *)lastSyncDate {
    //Something happens
    return lastSyncDate;
}

And then the _lastSyncDate identifier gives out an unidentified identifier error.

I'm not sure why this is happening?

Was it helpful?

Solution

It should be

return _lastSyncDate;

in the getter method. By default, the synthesized instance variables have a leading underscore (and you already use that in the setter method).

Also, if you provide both setter and getter method, you have to synthesize the property explicitly:

@synthesize lastSyncDate = _lastSyncDate;

The same would happen if you provide the getter for a read-only property. The property is synthesized by the compiler only if a required accessor method is missing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top