In Objective-C on iOS, what is the (style) difference between “self.foo” and “foo” when using synthesized getters?

I have searched many questions on ObjC accessors and synthesized accessors to no avail. This question is more of a “help me settle an issue” question; I don’t expect one answer, but I’m rather looking for experts to weigh in on the argument.

In a Cocoa Touch class, I would write some code like this (where soundEffects is a synthesized NSArray property):

  • Calling a method from another class in Objective C
  • How do you determine the size/frame of a custom UINavigationItem.titleView?
  • Check what version of the app is being used
  • How do I save a gif to my photo album?
  • scale Image in an UIButton to AspectFit?
  • KVO broken in iOS 9.3
  • id foo = [self.soundEffects objectAtIndex:1];
    

    A colleague asked me to explain why the above is any better than this line:

    id foo = [soundEffects objectAtIndex:1];
    

    Well, functionally, it’s no different.

    My arguments for the former are as follows:

    1. self.soundEffects tells every other coder working on the code that this is an iVar, not a locally scoped variable.

    2. If we ever needed to, we could put custom logic in the soundEffects getter accessor.

    3. For no concrete reason, it “feels” like the right thing to do after working in Obj-C for a year.

    He accepts arguments #1 and #2 as valid, but also gives the counterpoint:

    1. Isn’t this just code bloat?

    2. Shouldn’t a class be allowed to talk to its own iVars directly without having to call a method (the getter) on itself?

    Any takers?

    4 Solutions Collect From Internet About “In Objective-C on iOS, what is the (style) difference between “self.foo” and “foo” when using synthesized getters?”

    Your point 1 is not quite right: self.soundEffects is not an ivar, although it may happen to give you something which is — as it does in the case of your synthesized NSArray, at the moment.

    This in turn implies that your point 2 is the crux of the matter — if you route all access through the accessor, then everything is nicely encapsulated and you’re free to modify the implementation later without having to worry about side effects.

    It’s also good practice for when you use the mutator, so you maintain consistent memory management.

    For the most part, I’d say it’s advisable to route through self.property for everything that is a property, and restrict direct ivar access to things which are strictly internal. However, I’ll admit that in some cases — especially for things that don’t use retain/copy semantics — it can be more of a style preference.

    I have personally settled on using an underscore prefix for ivars, and this kind of synthesize

    @synthesize name = _name;
    

    That way I don’t mix them up. The major issue with not using self is that this code

    _name = ...
    

    is very different from

    self.name = ...
    

    When the @property uses the retain option. The first does not retain the object, and the second calls the synthesized setter that retains.

    The only time it makes a big difference is with assigning, so I tend to use self. all of the time so I make sure I do it on assigns.

    using something like self.variable = nil makes the variable go through its setter and therefore gets memory managed for free. if you just use variable = nil for instance in a dealloc method, it will cause a leak since it is not actually going through the synthesized setter for the variable and decreasing the retain count. See this post memory leak with self.

    For this reason, it is advisable (i believe) to always use self. when dealing with instance variables that you own as far as memory management is concerned.

    self.soundEffects

    sets/gets the instance variable through the setter/getter, and hence if we want to do some custom operations when their value changes, that logic can go in their getter/setter.

    Also as per ios 6, the ivar corresponding to

    @property (nonatomic)NSArray *propertyName;

    will be

    _propertyName

    so i guess you cant use

    id foo = [soundEffects objectAtIndex:1];

    anymore.Not sure though.Instead you should use

    id foo = soundEffects[1];