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 trialKaran Khare
3,477 Pointsself & .
sometime we add "self" keyword to call and sometimes we add"_", why its so.
#import "UIViewController.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) NSString *shoppingCart;
@property (strong, nonatomic) NSArray *shoppingList;
@end
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Add your code below!
self.shoppingList = @[@"toothpaste",@"bread",@"eggs"];
}
@end
3 Answers
Patrick Cooney
12,216 PointsWhen you use self.
you're calling the pre-built setter. It's the same as if you created a method like the following
-(void) setShoppingList:(NSArray *)list {
_shoppingList = list;
}
using self.
is the safe way to do it because you're using the setter rather than directly changing the ivar value in memory. Basically under the hood Xcode is setting the ivar value for you though.
Karan Khare
3,477 Pointscan you please elaborate a little more with a better example. Help is much appreciated Thanks
Patrick Cooney
12,216 PointsThe best way I can describe it is _variable
is accessing the value directly in the memory and self.variable
is using the setter method. Maybe this post will help a bit more? (It's long but it might be just what you need).