Use of IOS strong and weak

Source: Internet
Author: User

Strong decorated properties are strong pointer types, and weak decorated properties are weak pointer types.

Arc for an in-memory object management mechanism, when an object is not pointed to by a strong pointer, the object will be destroyed.

So inappropriate use of strong and weak modifiers can easily cause memory leaks.

The OC object (the object created by inheriting the NSObject Class) is decorated with strong.

UI controls are decorated with weak when wired, giving everyone an illusion. Is that UI control properties are decorated with weak. This is a wrong argument.

When we connect, storyboard will determine whether or not to use weak retouching, based on the structure of the layer, to verify that:

Any one controller will have a root view:

In the form of stroyboard the expression is

Therefore, the current layer structure is

Each uiuiew is a container that can hold other uiview, so UIView has a subviews attribute

Subviews is an array. An array is a collection, and when we store an object in a collection, the collection has a strong pointer to that object.

Copy here involves deep copy and shallow copy knowledge. A deep copy creates a new object and assigns the address of the new object to the Subviews property

A shallow copy does not create a new object, the knowledge copies the address of the current array object, and assigns the address to the Subviews property. Whether it is a deep copy, a shallow copy. Subviews are a strong pointer.

So, the current level of relationship is

So, when we drag the control to the Stroyboard. The hierarchical representation in storyboard is

That

At this point, Viewcontroller already has a strong pointer pointing indirectly to the Uiswitch object. When we connect, set the properties of the Viewcontroller, and then modify the switch property with strong at this time.

It's going to happen.

Viewcontroller a strong reference to the same object two times and does not conform to the ARC memory management mechanism. So after storyboard analysis, by default we use weak to decorate the properties of the lines.

That

At this point, the layer structure is

Below, I remove the view that comes with the controller. self.view= Nil At this point, so the strong pointer is broken.

This time, I dragged a imageview control,

At this time, ImageView is not pointed by a strong pointer, so when we give ImageView drag line, storyboard analysis, Viewcontroller no strong reference to ImageView, At this point the Viewcontroller requires a strong pointer pointing.

So storyboard would suggest we use strong pointers. The hierarchical relationship at this time is

So, when we drag the line to ImageView:

Therefore, UI controls are not necessarily decorated with weak by wiring. Interview often will be asked, be sure to remember!!! Don't fall into the interviewer's pit.

Use of IOS strong and weak

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.