2

我的开发环境:

Xcode 4.6.2

非自动引用计数

例如,假设我们有一个名为 的视图控制器CertainViewController,其中声明了一个名为的NSArray *类型化属性listData,具有属性retainnonatomiclistData将被加载到表格视图中。我会这样做:

// 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;
}

而其他一些开发人员会明确指定_listData属性的实例变量listData并这样做:

// 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];
}

以上两种实现是否完全等价?还是有什么细微的差别?显式指定实例变量_listData只是意味着要使用[_listData release];

欢迎任何提示,感谢您的帮助:D

4

2 回答 2

2

它们本质上是相同的,但是如果您想知道确切的区别,请查看文档:http: //developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

于 2013-05-24T02:16:59.917 回答
2

使用最新的 clang 编译器,您无需声明实例变量,编译器会自动为您完成。

实际上你甚至不需要再声明@synthesize了。一旦你声明了你的@property,编译器就会生成 getter、setter 和实例变量,就像你写的一样:

@synthesize listData = _listData;
于 2013-05-24T02:25:22.557 回答