ios--a reasonable definition of the object's reference type Strong/weak/assign/copy

Source: Internet
Author: User

Be sure to use the object's reference type appropriately in iOS:

Best practice: Any object in iOS has only one strong reference

Use the weak case:
1, lazy loading method needs to use weak decoration:
such as @property (Nonatmic,weak) iboutlet UILabel *lbl;
Note: The so-called lazy loading is a charge that is dragged through IB into the root view, and then wired to correspond to the attributes in the Viewcontroller. In this way, the equivalent of Self.view executes the Addsubview () method, that is, Self.view makes a strong reference to the control, then only weak references are required in Viewcontroller.

Use the strong case:
1, non-lazy loading (need to alloc+init by code)
eg. the control was not dragged into IB, the control was declared in the H file, and the control object was initialized in the M file. This situation requires the use of strong references at the time of declaration. namely: @property (Nonatmic,strong) iboutlet UILabel *lbl; The corresponding subsequent application has its initialization method: LBL = [[UILabel alloc] init];

Use the Assign case:
1, for the int, double, float, BOOL and other numerical object definition.
[Email protected] (nonatomic,assign) BOOL TF;

When using copy:
1, for the nsstring type of object definition:
[Email protected] (nonatomic,copy) NSString *strname;

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ios--a reasonable definition of the object's reference type Strong/weak/assign/copy

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.