Search code examples
iosuiviewrelease-managementretaincount

Retain Count and removeFromSuperview


I have a UIView that when I initialize it has already retain count 2 and I do not understand why, as a result I can not remove it with removefromsuperview

ViewController.h

  @property (nonatomic, retain)FinalAlgView * drawView;

ViewController.m

  self.drawView =[[FinalAlgView alloc]init];

 NSLog(@"the retain count 1 of drawView is %d", [self.drawView retainCount]);
 //the retain count 1 of drawView is 2

 [self.bookReader addSubview:self.drawView];

 NSLog(@"the retain count 2 of drawView is %d", [self.drawView retainCount]);
 //the retain count 2 of drawView is 3

 [self.drawView release];

 NSLog(@"the retain count 3 of drawView is %d", [self.drawView retainCount]);
 //the retain count 3 of drawView is 2

 [UIView animateWithDuration:0.2
                 animations:^{self.drawView.alpha = 0.0;}
                 completion:^(BOOL finished){ [self.drawView removeFromSuperview];
                 }]; 
 //do not remove

I not use ARC


Solution

  • You cannot count on retainCountyou will get confusing result, and better don't use it at all.

    From Apple:

    ... it is very unlikely that you can get useful information from this method.