UI controls are decorated with weak or strong

Source: Internet
Author: User
#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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.