Say this is in my header file:
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (weak) IBOutlet NSSlider *slider;
- (void)doSomething;
@end
…and this is the *m:
@implementation AppDelegate
- (void) doSomething {[self.slider setFloatValue:0];}
@end
I'm new to Xcode and Objective C, and I would like to use and understand the modern “tools” presented by Apple in its documentation, namely ARC, or here more specifically the ability to skip @synthesize
.
If I understood correctly, @property (weak) IBOutlet NSSlider *slider;
does a few things for me, including:
- creating an instance variable called
_slider
; - creating a getter named
slider
; - creating a setter named
setSlider
.
I've been doing C++ until now, so I see instance variables as the equivalent of what I am calling (rightly, I hope) members of my C++ classes, which I incidentally always call _member
.
I understand that the whole point of encapsulation is that, if you're outside a given class, you have to use accessors to access those variables; they are private to you, so there's no way you can access them via _member
even if you tried.
But, when I'm writing my *.m file of my class, _member
means something. Back to my example, I think self.slider
and _slider
are equivalent. The latter comes naturally to mind first, as it saves a few character.
My question is: are the two absolutely equivalent?
I know this looks similar to this question, for example, but here’s a few reasons why I wanted to ask myself:
- I don’t use
@synthesize
, so I’m really not the one creating_slider
, and I wonder if this makes a difference (I believe this is a fairly recent improvement of ObjC, and most answers still refer to@synthesize
); - it seems that on average, most conversations end up with “so, just use
self.name
”, but I don’t grasp if this is just a recommendation, a convention, of something more important (with an impact on, say, the performance); - similarly, some say you should only use
_name
in methods likedealloc
and its friends; but I don’t write those thanks to ARC, so does this mean I should never use_name
? If so, why?
I hope this justifies this post, I apologies if I missed a preexisting answer. Thanks in advance for your help.