6

I am quite new to Objective C and iOS, currently trying to learn app development using the iOS 6 SDK. One concept I really can't wrap my head around is the difference between "_variable" and "self.variable" when being accessed in the .m file. Are they the same? Or different?

Following is a simple sample

MyClass.h

#import <Foundation/Foundation.h>

@interface MyClass : NSObject
@property (strong, nonatomic) NSString *myName;
@end

MyClass.m

#import "MyClass.h"

@interface MyClass ()
@property (nonatomic, strong) NSString *anotherName; 
@end

@implementation MyClass
- (void) myFunction {
    _myName = @"Ares";
    self.myName = @"Ares";

    _anotherName = @"Michael";
    self.anotherName = @"Michael";
}
@end

So is there a difference in the above implementations to set a variable? Variable "myName" is Public while "anotherName" is Private.

Would greatly appreciate any inputs. Thanks!

ares05
  • 177
  • 4
  • 9
  • 2
    possible duplicate of [Difference between self.ivar and ivar?](http://stackoverflow.com/q/4142177/), [Difference between self and normal variable](http://stackoverflow.com/q/536388/), [Properties and accessors](http://stackoverflow.com/q/6085080/), [Ivar property, access via self?](http://stackoverflow.com/q/4088801/), [When to access properties with self](http://stackoverflow.com/q/4271657/), [What is the (style) difference between “self.foo” and “foo” when using synthesized getters?](http://stackoverflow.com/q/3494157/), and [many more](http://stackoverflow.com/search?q=%5Bobjc%5D+self.ivar) – jscs Feb 28 '13 at 07:40
  • 2
    Actually this question is not a duplicate. As a beginner, I searched for difference between _variable and self.variable. I would ignore the questions that don't use the underscore, so this was the only one for me. But, @Josh , your comment helped me anyways, to understand they're the same thing. – Arjan Feb 20 '14 at 09:18
  • Underscores don't change the nature or functionality of ivars in the slightest: [How does an underscore in front of a variable in a Cocoa/ObjC class work?](http://stackoverflow.com/q/822487) – jscs Feb 20 '14 at 19:22

1 Answers1

15

The difference is that:

the variable names with _ are instance variables.

self.variable is calling a getter method on your object.

In your example, the instance variables are automatically generated and you don't need to synthesize your properties either.

The real important difference in your example comes into play if you are not using ARC-

self.variable will retain an object for you if you mark the property with retain or strong _variable does not address memory management at all

Jesse Black
  • 7,966
  • 3
  • 34
  • 45
  • Using braces in your example is potentially confusing to a beginner! I'd stick with `self.variableName` – jrturton Feb 28 '13 at 07:34
  • 2
    Can you explain your last sentence? "`self.variable` will retain an object for you if you mark the property with `retain` or `strong` `_variable` does not address memory management at all" – 0xKayvan Aug 13 '15 at 08:19