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):
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:
self.soundEffectstells every other coder working on the code that this is an iVar, not a locally scoped variable.
If we ever needed to, we could put custom logic in the
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:
Isn’t this just code bloat?
Shouldn’t a class be allowed to talk to its own iVars directly without having to call a method (the getter) on itself?
- Implement own setter or use KVO?
- Override setter method and get “Local declaration of xxx hides instance variable”
- Difference between @interface definition in .h and .m file
- Please explain Getter and Setters in Objective C
- Why does a readonly property still allow writing with KVC
- Error: Variable with getter/setter cannot have an initial value
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
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.
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;
so i guess you cant use
id foo = [soundEffects objectAtIndex:1];
anymore.Not sure though.Instead you should use
id foo = soundEffects;
- ios swift: Is it possible to change the font style of a certain word in a string?
- How to get JSON response array all index values?
- Does “let _ = …” (let underscore equal) have any use in Swift?
- Converting to Swift 3 (Swift and Firebase Project)
- Stop dispatch_after
- What does the & symbol mean in Objective-C?
- add SKAction to Sprite queue run one after another
- NSFetchedResultsController custom sort not getting called
- So a CALayer does not contain a content bitmap of a view?
- Duplicate symbol error when adding NSManagedObject subclass, duplicate link
- Optimize loading of many images on iPad
- How to make iAds work on a 3.0 + iPhone-iPad app?
- iOS: How to use images in custom bundle in Interface Builder?
- Does the AppDelegate initialize when Healthkit wakes my app in the background?
- Create an (repetitive high pitch) Alarm on a remote trigger when App is not running (iphone/android) just like Find My iPhone