Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Fahad Alrahbi
1,080 Pointswhat is the difference between self and underscore objective c
hello all , i don't really understand what is the difference between using this
_myVar= 50;
and self.myVar = 50;
i have objective-c Code this code have 2 methods
first one look like
return self.myVar ;
other is
_myVar = 20;
the point is if i change the first method to
return _myVar ;
it will not work
i don't know why ! if possible i need clear explanation about this point
Thank You
3 Answers
Chris Shaw
26,676 PointsHi Fahad,
In Objective-C you need to initialize your class variables by hand as the compiler doesn't infer data types for the values you give them which can lead to unexpected results as a nil value could occur resulting in an app crash. To solve this we use a variable initialization which is where our friend the underscore comes into effect.
The underscore refers to a memory pointer that the Objective-C compiler assigns to the class during run-time, within our variable initialization we have access to this (magic) variable so we can assign the initial value to it otherwise return it if the variable has already being assigned a value other than nil which occurs when using self.variableName.
So as an overview this is what happens.
self.variableName -> _variableName
Hope that clears things up.
Fahad Alrahbi
1,080 PointsThanks for answer , so can i say i use , _myVar if i want assign value to myVar and self.myVar if i want to only read that value ?
Chris Shaw
26,676 PointsThe (magic) _variableName is only accessible within the initialization step, anytime you want to read or update the value you always use the class variable self.variableName you set in your implementation file.
Fahad Alrahbi
1,080 Pointsthanks Chris but still i will show you my example , I'm following sprite kit tutorial and i have this case
header file .h
#import <SpriteKit/SpriteKit.h>
@interface SpeacCatNod : SKSpriteNode
+(instancetype)speaceCatAtPosition:(CGPoint)position;
-(void) preformTap;
@end
And this is implementation file .m
@implementation SpeacCatNod
+(instancetype)speaceCatAtPosition:(CGPoint)position;
{
SpeacCatNod*speaceCat =[self spriteNodeWithImageNamed:@"spacecat_1"];
speaceCat.position=position;
speaceCat.anchorPoint=CGPointMake(0.5, 0);
speaceCat.name=@"SpeacCat";
return speaceCat;
}
-(void) preformTap{
[self runAction:self.tapAction];
}
- (SKAction *) tapAction {
if ( _tapAction != nil )
{
return _tapAction;
}
NSArray *textures = @[[SKTexture textureWithImageNamed:@"spacecat_2"],
[SKTexture textureWithImageNamed:@"spacecat_1"]];
_tapAction = [SKAction animateWithTextures:textures timePerFrame:0.25];
return _tapAction;
}
@end
look at preformTap method , you will find that teacher using self to return the value and on tapAction method he used _tapAction and if i change preformTap method to _ or i change tapAction method to self , not working , if possible explain me what happen in my example .
And thanks again Man !