#import "ViewController.h" @interface Viewcontroller () @property (nonatomic, weak) UIView *weakview;
@property (nonatomic, weak) UIView *selfweakview;
@property (nonatomic, strong) UIView *strongview;
@property (nonatomic, strong) UIView *selfstrongview;
@end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload];
Self.selfstrongview = [[UIView alloc]initwithframe:cgrectmake (25, 25, 50, 50)];
printf ("Self.selfstrongviewretain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (Self.selfstrongview)));
[Self.view AddSubview:self.selfStrongView];
printf ("Self.selfstrongview Retain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (Self.selfstrongview)));
_strongview = [[UIView alloc]initwithframe:cgrectmake (100, 100, 50, 50)];
printf ("_strongview Retain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (_strongview)));
[Self.view AddSubview:self.strongView]; printf ("_strongview Retain count =%ld\n", Cfgetretaincount ((__brIdge cftyperef) (_strongview));
UIView *selfview = [[UIView alloc]initwithframe:cgrectmake (150, 150, 50, 50)];
Self.selfweakview = Selfview;
printf ("Self.selfweakview Retain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (Self.selfweakview)));
[Self.view AddSubview:self.selfWeakView];
printf ("Self.selfweakview Retain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (Self.selfweakview)));
UIView *weaksview = [[UIView alloc]initwithframe:cgrectmake (200, 200, 50, 50)];
printf ("Weakview Retain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (Weaksview)));
_weakview = Weaksview;
printf ("_weakview Retain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (_weakview)));
[Self.view Addsubview:_weakview];
printf ("_weakview Retain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (_weakview))); }-(void) Viewwillappear: (BOOL) Animated {printf ("self.selfstrongview Retain count =%ld\n", Cfgetretaincount (__BRIDG e cftyperef) (_str(Ongview)));
printf ("_strongview Retain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (_strongview)));
printf ("Self.selfweakview Retain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (Self.selfweakview)));
printf ("_weakview Retain count =%ld\n", Cfgetretaincount ((__bridge cftyperef) (_weakview))); } @end
in Arc, the final basis on which the object is disposed or released according to the reference count is 0 o'clock. The fundamental difference between weak and strong is that in the set method, the weak set method and the strong set method both release the old value to retain the new value, but the weak set method autorelease it, that is, delay release once, The set method of strong also frees the old value to retain the new value, but it does not delay release. The end result is that strong will +1,weak not +1.
Note that when you use _ to assign a value is not called the set method, that is, whether weak or strong, as long as the value of _ will not add 1 to the reference count, the difference is self. The syntax calls the set method, strong self. Call set method +1. and Weak's _ and self. Grammar is not +1.
Therefore, it is recommended to use weak, with the weak, both _ and. Syntax will not cause +1. Use self when using strong. Syntax can cause +1. recommend using weak. Also weak the object pointer to nil after the object is recycled.