iOS development--use of @property properties

Source: Internet
Author: User

1, strong and weak

Strong: Strong pointer/strong reference; By default, any pointer is a strong pointer; that is, we usually define the person *p =[[person alloc] init]; all strong;
Weak: weak pointer/weak reference; A pointer decorated with _weak.

ARC (auto reference count) criterion: This object will be destroyed as long as there is no strong pointer pointing;
Of course more than their own scope will be destroyed;

But after the program executes, there is still a strong pointer pointing (your strong attribute points to me, my strong attribute points to you), which causes a memory leak

@autoreleasepool {//starts with a weak pointer, this line of code is dropped, the P object is destroyed (called Dealloc method) _weak Person *p = [Person alloc] init]; }

@autoreleasepool {person  *p = [Person alloc] init];   Dog *d = [Dog alloc] init];  P.dog = D;  D = nil; is equivalent to removing the pointer to the D object in the heap memory  
Ordinary objects do attribute use strong: This ensures that the current object P in, P's dog attribute points to the object also in

2. Why do UI objects use weak?

For UI objects, weak and strong are fine, but there is no need to use strong.
The View property in Uiviewcontroller is a strong pointer: @property (nonatomic,retain) UIView *view; (Retain was strong before iOS5)
In the view there is another subviews array, the array is a strong pointer, 0 of the position of the strong pointer to the UIButton;
So VC in--view on the--subviews in the--0 location of the strong pointer is in, pointing to the UIButton object is in, so no UI control is not necessary to define strong
Assign before IOS5 was weak.

3, delegate why use weak?

VC in the View property is strong, point to view, if the view delegate is strong, generally point to the VC, two root is strong will be a problem (memory leak), so delegate must be weak

4. This picture is a good explanation of the above questions:


For example delegate why use weak?

If delegate uses strong, when the program exits, the UIWindow object is destroyed, and the Uitableviewcontroller object is destroyed.

However, because the delegate is strong and points to the VC, so the VC will not be destroyed, causing memory leaks.

5. Use of @property attributes

Weak (assign): Proxy \ui control
Strong (Retain): normal object
Copy: String
Assign: Basic data type









iOS development [email protected " property use

Related Article

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.