Until five minutes, I was sure that my understanding of Objective c reference counting was excellent, but when I started checking keepCount objects, I was very surprised to see what I saw.
For example, myViewController has a UITableview:
.h file
@interface RegularChatViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> { UITableView *_tableView; } @property (nonatomic, retain) IBOutlet UITableView *tableView;
.m file
@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]); }
To my surprise, the entrance was:
tableView retain count: 1 tableView retain count: 2 tableView retain count: 3
obviously STEP ONE increment counter by 1 using alloc
I also know that STEP THREE will increment the counter by 1 using addSubview
But what happens in STEP TWO ??? why did it increase the number of deductions?
is there anything in common with ARC ??
source share