Question

maybe it's a little childish question, but I really want to know the detail. I've just seen this code:

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

what's the difference between this and the following one?

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

@implementation SimpleMainViewController
@synthesize simpleTableViewController;
@synthesize aboutViewController;

Thanks in forward.

Était-ce utile?

La solution

The first one is only visible and acceseable from inside the implemented class. It is called an instance variable.

Whereas the property is visible to other classes as well. A property is backed by an iVar too. The @synthesize is doing this behind the scenes. In your case the backing iVar would be accessable with the name of the property (e.g. simpleViewController). But one should access a property via self (e.g. self.simpleViewController) for simpler memory management and to distinguish it from an normal iVar. The @synthesize will generate getter and setters to the iVar and will do memory management according to the property declaration (here retain).

Nowadays you do not even need a @synthesize any more. Just declare a property. The compiler will create the property with an backing iVar with a prefix underscore. So one could access it either via self.simpleTableViewController or via _simpleTableViewController.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top