If we have .h file like this:
@interface TestViewController : UIViewController {
__weak NSObject *object;
}
@end
and methods in .m file like this:
- (void)viewDidLoad {
[super viewDidLoad];
NSObject *localObject = [[NSObject alloc] init];
NSLog(@"%ld", CFGetRetainCount((__bridge CFTypeRef)localObject));
object = localObject;
NSLog(@"%ld", CFGetRetainCount((__bridge CFTypeRef)object));
NSLog(@"%ld", CFGetRetainCount((__bridge CFTypeRef)localObject));
}
Then we get the following output for retain count:
1
2
1
My question is why does the retain count gets incremented to 2 on "object" when it is declared as "__weak" instance variable, furthermore "localObject" retain count remains 1. I think that's because of how ARC inserts retain/release but I am not sure.