Take the 2-minute tour ×

This question already has an answer here:

I am writing class in Objective c.

and I want a private property from type double in my main view controller.

my code: MainViewController.m

#import "MainViewController.h"

@interface MainViewController ()

@property (nonatomic) BOOL x;


@implementation MainViewController

now , i know taht Automatic Property Synthesis With Xcode 4.4 but if i dont write @synthesize x; i can access to my property "x" only with "self.x" or "_x"

and if i write @synthesize x

i can access with just write "x".

i know it is a simple question ,but I really want to understand what is happening in the code behind, and the essential difference between the two approaches.


share|improve this question

marked as duplicate by DrummerB, Monolo, Josh Caswell, bbum, jlehr May 12 '13 at 21:58


The default is actually @synthesize x = _x; by saying @synthesize x; you are really saying @synthesize x = x; Does that make sense? –  Justin Meiners May 12 '13 at 16:32
@DrummerB not that one, but im sure this question has been asked –  Justin Meiners May 12 '13 at 16:32
I explained all this in my answer to that question, that's why I linked it. –  DrummerB May 12 '13 at 16:35

1 Answer 1

up vote 1 down vote accepted

If you write @synthesize x; you can either use self.x or x

If you don't synthesize the property the compiler does behind the scenes this: @synthesize x = _x. Now you can either use self.x or _x

But you can synthesize and assign the ivar a different name as well, e.g. @sythesize x = _aBoolen —now you can use self.x or _aBoolean

share|improve this answer
Note that self.x and _x as accessors or setters are quite different in behavior; the former is a KV observable method call, the latter is not. –  bbum May 12 '13 at 17:32
