在interface文件中用下面三种方法:
方法一:
@interface Application
{
UserInfo* userInfo;
ApplicationInfo*applicationInfo;
}
@property (retain) UserInfo*userInfo; // @synthesize userInfo;
@property (retain) ApplicationInfo* applicationInfo ; // @synthesize applicationInfo;
@end
方法二:
@interface Application
{
}
@property (retain) UserInfo*userInfo; // @synthesize userInfo;
@property (retain) ApplicationInfo* applicationInfo ; // @synthesize applicationInfo;
@end
方法三:
@interface Application
{
UserInfo* userInfo;
ApplicationInfo*applicationInfo;
}
@end
以上三种方法在implement中都可以使用 userInfo 和 applicationInfo,具体有什么区别?
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.