code1
@interface Rectangle : NSObject
{
int width,height;
}
@property int width,height;
code2
@interface Rectangle : NSObject
@property int width,height;
{
int width,height;
}
Can anyone tell me, what is different between them? And in @property
I already put int
for width
and height
. Why do we need to declare it again in curly brackets?
Update: I am using osx 10.8 64bit.
In your example "code1" is correct but possibly unneeded, while "code2" is incorrect and won't compile. Any instance variables need to be declared in curly brackets directly after the @interface line which is why the second example is wrong.
As some others has mentioned you may not need to declare width
and height
at all. If you are using the modern runtime the compiler can tell from the @property
statement that you meant to have an underlying instance variable for the property and will create these for you. On the other hand with the older legacy runtime the instance variables like width
and height
must be explicitly declared. You didn't specify your platform so it is no clear if it is needed in your case.
The modern runtime is always used on iOS, which is why many people here will tell you incorrectly that the instance variables do not need to be declared in any case. On Mac OS X the modern runtime is used with 64-bit code in 10.5 or newer. If you are using 32-bit code or an older version you need to explicitly declare your instance variables.