2

My development environment:

Xcode 4.6.2

non-Automatic Reference Counting

For example, suppose we have a view controller called CertainViewController, where a NSArray * typed property called listData has been declared, with attributes retain and nonatomic. listData is to be loaded to a table view. I would do it like this:

// CertainViewController.h
@interface CertainViewController : UIViewController
{
}
@property (retain, nonatomic) NSArray *listData;

// CertainViewController.m
@implementation CertainViewController

@synthesize listData;

- (void) viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // someValue is ready
    self.listData = someValue;
    // Release someValue
}

- (void) dealloc
{
    self.listData = nil;
}

While some other developers would explicitly specify the instance variable _listData for property listData and do it like this:

// CertainViewController.h
@interface CertainViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    NSArray *_listData;
}

@property (retain, nonatomic) NSArray *listData;

// CertainViewController.m
@implementation CertainViewController

@synthesize listData = _listData;

- (void) viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // someValue is ready
    self.listData = someValue;
    // Release someValue
}

- (void) dealloc
{
    [_listData release];
}

Are the above two implementations totally equivalent? Or any tiny differences? The explicit appointing instance variable as _listData just mean to be use [_listData release];?

Any hint is welcome and thank you for help :D

Community
  • 1
  • 1
George
  • 3,384
  • 5
  • 40
  • 64

2 Answers2

2

They're essentially the same, but if you want to know the exact difference, check out the documentation: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

Matt Becker
  • 2,338
  • 1
  • 28
  • 36
2

With the latest clang compiler you don't need to declare the instance variable, the compiler does it for you automatically.

Actually you don't even need to declare @synthesize anymore either. As soon as you declare your @property, the compiler will generate the getter, setter and the instance variable like if you had written:

@synthesize listData = _listData;
Sylvain Guillopé
  • 1,358
  • 9
  • 21