一个alloc,retainCount == 2

3 iphone memory-management

我有以下代码:

NSLog(@"%d", [chart retainCount]);

self.chart = [[BNPieChart alloc] initWithFrame:self.view.frame];
NSLog(@"%d", [chart retainCount]);
Run Code Online (Sandbox Code Playgroud)

终端显示:

[Session started at 2011-03-28 11:09:46 +0200.]
2011-03-28 11:09:51.008 Finance[35111:207] 0

2011-03-28 11:09:51.010 Finance[35111:207] 2
Run Code Online (Sandbox Code Playgroud)

据我所知,retainCount应该等于1,而不是2.

une*_*lue 6

您将图表属性定义为保留或复制,因此:

self.chart = [[BNPieChart alloc] initWithFrame:self.view.frame];
Run Code Online (Sandbox Code Playgroud)

+1保留在alloc([BNPieChart alloc])
+1保留在赋值(self.chart =)