1

I notice that some samples of code using the following syntax:

@synthesize myProp = _myProp;

How is that different than just doing this?

@synthesize myProp;

And later in the code they call it by doing this:

self.myProp = xyz;

Why not just call it this way?

mpProp = xyz;

Can someone explain the differences?

CodaFi
  • 43,043
  • 8
  • 107
  • 153
Ethan Allen
  • 14,425
  • 24
  • 101
  • 194
  • @CodaFi - I know what they DO... I am asking what the different syntax is for... one vs the other. Your "duplicate" is not a duplicate of this question. – Ethan Allen Jun 14 '13 at 00:31
  • Property access syntax is covered in the ObjC language spec from Apple and [this question](http://stackoverflow.com/questions/2297646/how-does-dot-syntax-work-in-objective-c). – CodaFi Jun 14 '13 at 00:40
  • 1
    Calling with `self.` calles the setter/getter method, directly accessing the ivar (`_myProp = ...`) does not. – zaph Jun 14 '13 at 00:56

2 Answers2

3

The main advantage of doing this is that you're using getters and setters when accessing the properties. This is important because:

  • Setters manage memory semantics automatically, retaining and releasing object references automatically.
  • Automatically get you KVO semantics, which is important in several different contexts

The first reason is less important with ARC, but the latter is still a pretty large benefit.

SplinterReality
  • 3,400
  • 1
  • 23
  • 45
  • 2
    It is not necessary to even use `@synthesize` anymore, the compiler now automatically creates the ivar and setter/getter in the manner the OP wrote. i.e.: @synthesize myProp = _myProp;` – zaph Jun 14 '13 at 00:53
3

There are two parts to your question.

Part 1:

Using self.myProp = xyz calls the setter of the property to set the data, instead of direct assigning myProp = xyz. It's advised to access and modify data, even private or internal data, by setters and getters. They give you a way of handling related issues with the data as @SplinterReality mentioned in his answer. But the related issues can be more than that, for example:

Lazy loading

- (SomeObject *) myProp {
    if(_myprop == nil) {
        _myProp = [[SomeObject alloc] init];
    }
    return _myProp;
}

Doing some related actions Note: This can also be handled by KVO. This is simpler.

- (void) setProperty:(SomeObject *)object (
    _property = object;
    id relatedProperty = //Contruct related value here.
    self.relatedProperty = relatedProperty;
    [self updateViewThatShowsPropertyContent];
}

Part 2:

Using @synthesize is used to create the default setters and getters of the property, i.e. you're not going to provide your custom implementation for them.

@synthesize myProp = variableName; makes the generated instance variable named _variableName. But the default behavior is generating an instance variable called _property for @property property. So @synthesize myProp = _myProp; and @synthesize myProp; aren't different.

However, in modern Objective-C used right now, @synthezie is not needed, i.e. it's called for you. So unless you need to provide an instance variable other than the default name, you don't need to call it.

Mohannad A. Hassan
  • 1,650
  • 1
  • 13
  • 24