r/ObjectiveC • • Jun 29 '22

Is there a difference between [self attributeName] and self.attributeName ?

Hello,

I'm an objective-C newbie, and I've got to work on some legacy code. A question I can't find a clear answer to is the difference between `[self attributeName]` and `self.name.`

So I declare a .h for a class, its attributes and methods and I want to interact with them in the .m. I usually feel more comfortable using `self.name` for assigning a value to the class's attribute and `[self attributeName]` for reading the value of the attribute, but I feel like they're totally interchangeable.

Am I correct or is there a real difference I'm missing ?

Thanks in advance !

8 Upvotes

8 comments sorted by

View all comments

Show parent comment

11

u/whackylabs Jun 29 '22

Properties in ObjC are just syntactic sugar. From the docs:

You can think of a property declaration as being equivalent to declaring two accessor methods. Thus

@property float value;

is equivalent to:

- (float)value;
  • (void)setValue:(float)newValue;

Notice, it isn't - (float)getValue