IOS -- reasonably define the reference type of the object strong/weak/assign/copy, ios -- assign

Source: Internet
Author: User

IOS -- reasonably define the reference type of the object strong/weak/assign/copy, ios -- assign

In ios, the reference type of the object must be reasonably used:

Best Practice: in ios, any object has only one strong reference

When weak is used:
1. weak must be used for lazy loading:
For example, @ property (nonatmic, weak) IBOutlet UILabel * lbl;
Note: the so-called lazy loading means that the control is dragged to the root view through IB, and then connected to the attributes in ViewController. This method is equivalent to self. view executing the addSubview () method, that is, self. view strongly references the control, so you only need to make a weak reference in viewController.

Use of strong:
1. Non-lazy loading (alloc + init through code)
Eg. Did not drag the control to IB, declare the control in the H file, and initialize the control object in the m file. In this case, you must use strong references when declaring them. That is: @ property (nonatmic, strong) IBOutlet UILabel * lbl; the corresponding application has its initialization method: lbl = [[UILabel alloc] init];

When using assign:
1. Definitions of int, double, float, BOOL, and other numeric objects.
Eg. @ property (nonatomic, assign) BOOL tf;

Use of copy:
1. Definitions of NSString objects:
Eg. @ property (nonatomic, copy) NSString * strName;

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.