以下状态有什么区别?

时间:2013-06-28 06:27:32

标签: ios objective-c synthesize

也许这是一个有点幼稚的问题,但我真的想知道细节。我刚刚看到这段代码:

@implementation SimpleMainViewController
{
    SimpleTableViewController *simpleTableViewController;
    AboutViewController *aboutViewController;
}

这和下一个有什么区别?

@interface SimpleMainViewController : UIViewController
@property(nonatomic,retain) SimpleTableViewController *simpleTableViewController;
@property(nonatomic,retain) AboutViewController *aboutViewController;

@implementation SimpleMainViewController
@synthesize simpleTableViewController;
@synthesize aboutViewController;

先谢谢。

1 个答案:

答案 0 :(得分:4)

第一个只在实现的类中可见并且可以加入。它被称为实例变量。

尽管该属性对其他类也是可见的。房产也由iVar支持。 @synthesize正在幕后进行此操作。在您的情况下,可以使用属性的名称(例如simpleViewController)访问支持iVar。但是应该通过self(例如self.simpleViewController)访问属性,以便更简单的内存管理,并将其与普通的iVar区分开来。 @synthesize将为iVar生成getter和setter,并根据属性声明(此处为retain)进行内存管理。

现在你甚至不再需要@synthesize了。只需声明一个属性。编译器将使用带有前缀下划线的支持iVar创建属性。因此,可以通过self.simpleTableViewController_simpleTableViewController访问它。