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?
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;
- I want to call 20 times per second the installTapOnBus:bufferSize:format:block:
- Swift: Adjusting the line height of UITextView
- How to set a custom font for entire iOS app without specifying size
- NSCache Doesn't work with all images when loading for the first time
- Get current scroll position of ScrollView in React Native
- Can I use Objective-C blocks as properties?
- How to change app display name in Xcode 8 to add a space
- Required background modes iOS6 Xcode 4.5
- WebRTC + IOS + Freeswitch : Can't hear audio
- How to add Images for different screen size from Assets.xcassets in Xcode 8?
- RestKit: Getting HTTP status code when a request fails
- HTML to NSAttributedString and NSAttributedString to HTML
- How to get localized Cancel, Done and etc?
- Impact of Xcode build options “Enable bitcode” Yes/No
- How to apply multiple filter in Firebase query in Swift?