【发布时间】:2012-05-28 22:02:33
【问题描述】:
直到五分钟,我才确定自己对 Objective c 引用计数的理解非常好,但是当我开始检查对象的 retainCount 时,我对所看到的感到非常惊讶。
例如 myViewController 有一个 UITableview:
.h 文件
@interface RegularChatViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
UITableView *_tableView;
}
@property (nonatomic, retain) IBOutlet UITableView *tableView;
.m 文件
@synthesize tableView = _tableView;
- (void)loadView
{
_tableView = [[UITableView alloc] init]; // STEP ONE
NSLog(@"tableView retain count: %d",[_tableView retainCount]);
self.tableView.frame = CGRectMake(0, 0, 320, tableHeight); // STEP TWO
NSLog(@"tableView retain count: %d",[_tableView retainCount]);
[self.view addSubview:self.tableView]; // STEP THREE
NSLog(@"tableView retain count: %d",[_tableView retainCount]);
}
令我惊讶的是,输入是:
tableView retain count: 1
tableView retain count: 2
tableView retain count: 3
显然 STEP ONE 使用 alloc 将保留计数增加 1
我也知道第三步使用addSubview 将保留计数增加 1
但是第二步发生了什么???为什么它增加了保留计数???
和ARC有关系吗??
【问题讨论】:
-
可能是因为
.frame没有table view就无法存在,因此增加了retain count? -
猜对了;但是
frame直接返回一个结构;甚至没有参考。那里没有依赖关系。
标签: iphone objective-c ios reference-counting retaincount