为什么retainCount增加了?

时间:2012-02-09 14:30:26

标签: objective-c

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    _testObject = [[TestObject alloc] init];

    NSLog(@"_testObject rc = %d", _testObject.retainCount);
    NSLog(@"testObject rc = %d", self.testObject.retainCount);
}

而testObject是

@property (retain) TestObject *testObject;

为什么这些NSLog() 1和2的输出分别是?属性的retainCount和底层对象是不同的?

1 个答案:

答案 0 :(得分:6)

我猜这是因为你的属性没有被定义为nonatomic,所以你得到一个保留/自动释放的对象。尝试将您的属性声明更改为:

@property (retain, nonatomic) TestObject *testObject;