Why counter variable equals 3, not 2?
@interface ScoreView : UIImageView
...
- (id)initWithFrame:(CGRect)frame
{
if (!(self = [super initWithFrame:frame]))
return self;
_scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 10, 10)];
[self addSubview:_scoreLabel];
int counter = [[[self subviews] objectAtIndex:0] retainCount]; // WHY 3?
return self;
}
-retainCountis not reliable.In your case, the particular reason is because
-subviewalso causes all subviews to be retained once, by copying the value of.layer.sublayersto a new array*:There is no need to worry about it, as the array is autoreleased and the retainCount will drop back to 2 later. All you need is to ensure is the net retain count cause by the current function is consistent with the ownership status.
*: The particular implementation of
.subviewsis: