Question

Recently I have started learning Objective-C, and I get puzzled about the member var and property. I want to know what's the difference between the following three code blocks:

1.

@interface Application 
{ 
    UserInfo* userInfo; 

    ApplicationInfo*applicationInfo;
}

@property (retain) UserInfo*userInfo; // @synthesize userInfo; 

@property (retain) ApplicationInfo* applicationInfo ; // @synthesize applicationInfo;

@end 

2.

@interface Application 

{ 
}

@property (retain) UserInfo*userInfo; // @synthesize userInfo; 

@property (retain) ApplicationInfo* applicationInfo ; // @synthesize applicationInfo; 

@end 

3.

 @interface Application 
{ 
    UserInfo* userInfo; 

    ApplicationInfo*applicationInfo;
} 

@end

Thank you very much!

Was it helpful?

Solution

1: This declares instance variables with accompanying declared properties. When you synthesise the properties, for example the property foo, the foo and setFoo methods are automatically generated which refer to the instance variables (due to having the same name), and these are called when you use dot notation (e.g. someInstance.foo = bar) to access them. Because you defined the properties to be retain, when you assign an object to the property using the setter or through dot notation, the existing object pointed to by the instance variable is released and the new object is assigned to the instance variable and retained.

2: Exactly the same as 1. The instance variables are implied.

3: The instance variables are there, but the properties are not, so you won't be able to access them through dot notation and no getter/setter methods are synthesised.

I strongly recommend reading the official documentation on this, it's quite clear and lays it all out in a fairly straightforward manner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top