23

Possible Duplicate:
Is there a difference between an “instance variable” and a “property” in Objective-c?
Difference between self.ivar and ivar?

What is the difference between declaring variables in brackets immediately after the @interface line, and defining properties below?

For example...

@interface GCTurnBasedMatchHelper : NSObject {
BOOL gameCenterAvailable;
BOOL userAuthenticated;
}

@property (assign, readonly) BOOL gameCenterAvailable;
Community
  • 1
  • 1
Alan
  • 4,325
  • 3
  • 21
  • 25

3 Answers3

23

Defining the variables in the brackets simply declares them instance variables.

Declaring (and synthesizing) a property generates getters and setters for the instance variable, according to the criteria within the parenthesis. This is particularly important in Objective-C because it is often by way of getters and setters that memory is managed (e.g., when a value is assigned to an ivar, it is by way of the setter that the object assigned is retained and ultimately released). Beyond a memory management strategy, the practice also promotes encapsulation and reduces the amount of trivial code that would otherwise be required.

It is very common to declare an ivar in brackets and then an associated property (as in your example), but that isn't strictly necessary. Defining the property and synthesizing is all that's required, because synthesizing the property implicitly also creates an ivar.

The approach currently suggested by Apple (in templates) is:

Define property in header file, e.g.:

@property (assign, readonly) gameCenter;

Then synthesize & declare ivar in implementation:

@synthesize gameCenter = __gameCenter;

The last line synthesizes the gameCenter property and asserts that whatever value is assigned to the property will be stored in the __gameCenter ivar. Again, this isn't necessary, but by defining the ivar next to the synthesizer, you are reducing the locations where you have to type the name of the ivar while still explicitly naming it.

RichieAHB
  • 2,030
  • 2
  • 20
  • 33
isaac
  • 4,867
  • 1
  • 21
  • 31
  • Declaring a property (not 'defining', by the way -- properties can't be defined in Objective-C) declares a getter and/or setter method, but does *not* generate accessor methods. Accessor methods are synthesized if and only if there's a corresponding `@synthesize` in the implementation, which is optional. Also, it's probably worth mentioning that variables declared inside curly braces are known as *instance variables*. – jlehr Mar 14 '12 at 13:15
  • On points of detail: Yes, it is @synthesize that actually generates the accessors. Yes, you "declare" properties (and then "define" their behaviors in the parenthesis). Given the scope of the question, I think it's fair to assume we know that "variable" refers to an instance variable (the answer uses "ivar" repeatedly). – isaac Mar 14 '12 at 14:08
  • 1
    Further elaboration: ObjC promotes encapsulation as a design strategy. Other classes shouldn't be able to directly mess with the internal state of an object (indeed, these days it's seen as best to show as few implementation details in your class' header file as possible). Instead, they can use getter/setter methods (such as those automatically created by the @property/@synthesize syntax) to access its declared properties. This allows the object to react to requests to return a value (say, by lazily loading it) or to set a new value (say, by also updating some corresponding UI). – rickster Mar 14 '12 at 16:09
  • @isaac, instead of responding with a comment, why not update your answer to improve it? – jlehr Mar 14 '12 at 17:48
15
{
BOOL gameCenterAvailable;
BOOL userAuthenticated;
}

the above two are called member Variables They can't be accessed outside the class.(Important point) (unless you provide custom getters and setters)

if you make a @property then the variable can be read inside the class as well as outside the class..so the setters and getters are generated for you..automatically

then declaring the same as a member variable isn't required..

It is just done to increase Readability .. you can read it easily than reading

 @property (non..) 
NguyenDat
  • 4,129
  • 3
  • 46
  • 46
Shubhank
  • 21,721
  • 8
  • 65
  • 83
2

When you define a property a getter and setter is created for you. When you access them usingobject.member setters and getters are called automatically.

When you declare variable in interface setters and getters are not written for you. you can also specify some visibility modifiers to them like @private,@public etc.

Vignesh
  • 10,205
  • 2
  • 35
  • 73