question 1: is the reference count for the object or the memory where the data is stored?
if it points to an object. But when decorated with Strong. I increased the reference count of T2 = t0 _ 0 _ t0 _ 0 _ T _ 1 ~ T _ 3 by 1 to 3, indicating that it is for memory.
for memory when speaking. But when you decorate T1 with weak, t0Magi T1 and T2 all point to the same memory address: 0x1c401eef0, but you can see from the log that their reference counts are different. So there is problem 1
question 2: when I modify T1 with weak, I put T1 = t0. Because of the weak modified T1, the reference count of t0 has not changed, it is still 1; but why does the reference count of T1 still change from + 1 to 2? (see log below)
-sharpimport "ViewController.h"
@interface RCTest ()
@property(nonatomic,copy)NSMutableString *s;
@end
@implementation RCTest
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
RCTest *t0 = [[RCTest alloc] init];
RCTest *t1;
RCTest *t2;
NSLog(@"\n");
NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
t1 = t0;
NSLog(@"\n");
NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
t2 = t0;
NSLog(@"\n");
NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
t1 = nil;
NSLog(@"\n");
NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
}
@end
Log:
2018-11-25 23:15:50.096611+0800 TestApp[2144:124995]
2018-11-25 23:15:50.096789+0800 TestApp[2144:124995] t0:0x1c4014b10,t1:0x0,t2:0x0
2018-11-25 23:15:50.096909+0800 TestApp[2144:124995] t0:1,t1:(null),t2:(null)
2018-11-25 23:15:50.096989+0800 TestApp[2144:124995]
2018-11-25 23:15:50.097051+0800 TestApp[2144:124995] t0:0x1c4014b10,t1:0x1c4014b10,t2:0x0
2018-11-25 23:15:50.097141+0800 TestApp[2144:124995] t0:2,t1:2,t2:(null)
2018-11-25 23:15:50.097216+0800 TestApp[2144:124995]
2018-11-25 23:15:50.097278+0800 TestApp[2144:124995] t0:0x1c4014b10,t1:0x1c4014b10,t2:0x1c4014b10
2018-11-25 23:15:50.098235+0800 TestApp[2144:124995] t0:3,t1:3,t2:3
2018-11-25 23:15:50.098356+0800 TestApp[2144:124995]
2018-11-25 23:15:50.098425+0800 TestApp[2144:124995] t0:0x1c4014b10,t1:0x0,t2:0x1c4014b10
2018-11-25 23:15:50.098522+0800 TestApp[2144:124995] t0:2,t1:(null),t2:2
when decorated with weak:
-sharpimport "ViewController.h"
@interface RCTest ()
@property(nonatomic,copy)NSMutableString *s;
@end
@implementation RCTest
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
RCTest *t0 = [[RCTest alloc] init];
__weak RCTest *t1;
RCTest *t2;
NSLog(@"\n");
NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
t1 = t0;
NSLog(@"\n");
NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
t2 = t0;
NSLog(@"\n");
NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
t1 = nil;
NSLog(@"\n");
NSLog(@"t0:%p,t1:%p,t2:%p",t0,t1,t2);
NSLog(@"t0:%@,t1:%@,t2:%@",[t0 valueForKey:@"retainCount"],[t1 valueForKey:@"retainCount"],[t2 valueForKey:@"retainCount"]);
}
@end
Log
2018-11-25 23:17:09.154880+0800 TestApp[2161:126259]
2018-11-25 23:17:09.155053+0800 TestApp[2161:126259] t0:0x1c401eef0,t1:0x0,t2:0x0
2018-11-25 23:17:09.155442+0800 TestApp[2161:126259] t0:1,t1:(null),t2:(null)
2018-11-25 23:17:09.155544+0800 TestApp[2161:126259]
2018-11-25 23:17:09.155610+0800 TestApp[2161:126259] t0:0x1c401eef0,t1:0x1c401eef0,t2:0x0
2018-11-25 23:17:09.155736+0800 TestApp[2161:126259] t0:1,t1:2,t2:(null)
2018-11-25 23:17:09.155811+0800 TestApp[2161:126259]
2018-11-25 23:17:09.155875+0800 TestApp[2161:126259] t0:0x1c401eef0,t1:0x1c401eef0,t2:0x1c401eef0
2018-11-25 23:17:09.155969+0800 TestApp[2161:126259] t0:2,t1:3,t2:3
2018-11-25 23:17:09.156042+0800 TestApp[2161:126259]
2018-11-25 23:17:09.156103+0800 TestApp[2161:126259] t0:0x1c401eef0,t1:0x0,t2:0x1c401eef0
2018-11-25 23:17:09.156195+0800 TestApp[2161:126259] t0:2,t1:(null),t2:2
-sharp-sharp-sharp
-sharp-sharp-sharp
//
-sharp-sharp-sharp