IOS Development note-strong and weak settings for the base UI (8) Control connection

Source: Internet
Author: User
Tags dashed line

Memory management of OC:

Objective-c syntax fast over (6) Memory management principles, OBJECTIVE-C syntax fast over (7) compiler features Arc

arc is an apple in order to simplify the programmer's memory management, introduced a set of memory management mechanism , using the arc mechanism, object application and release work will be at runtime, the compiler automatically add retain and release in the code

1> Strong: An object referenced by a strong pointer that is not released by the system during the life cycle . in OC, objects are strongly pointers by default

2> Weak: The object referenced by the weak pointer, the system is immediately released, Weak pointers can point to other objects that have already been referenced by a strong pointer

They're all arc's stuff.

ARC's Judging criteria:

As long as there is no strong pointer to the object, the object is freed, and the weak pointer does not, and a weak pointer points to the object in a timely manner, and the object does not have a strong pointer, and it is automatically freed. In general, you do not need to explicitly declare a strong pointer, but in the encapsulation, you need to specify when you define the method. A weak pointer, however, must be explicitly stated. The default is strong pointers.

ARC Features

1> does not allow calls to release, retain, Retaincount

2> allows rewriting of dealloc, but does not allow calls to [super Dealloc]

Parameters of the 3> @property

* Strong: Member variable is strong pointer (for OC object type)

* Weak: member variable is weak pointer (for OC object type)

* Assign: For non-OC object types

4> former retain changed to strong

OC pointers are divided into 2 types:

1> Strong pointer: By default, all pointers are strong pointers __strong

2> Weak pointer: __weak

In point m file

#import " Dashuai.h " @implementation Dashuai-(void) dealloc{    NSLog (@ " object was destroyed!) ");     // [Super Dealloc]; }@end

In the View controller

@interface Viewcontroller () @property (nonatomic, strong) Dashuai   *da; @end @implementation Viewcontroller-(void) viewdidload {    [super Viewdidload];     = [[Dashuai alloc] init];    NSLog (@ " called the Viewdidlowd Method!) ");} @end

object is not destroyed, DA is a strong pointer object.

If the declared object (which is a strong pointer by default) is inside the method, there is an issue with the declaration period (local variable). The execution of the method is complete, and the memory is automatically destroyed when the closing parenthesis is complete.

-(void) viewdidload {    [super Viewdidload];     // Self.da = [[Dashuai alloc] init];    Dashuai *dashuai = [[Dashuai alloc] init];    NSLog (@ " called the Viewdidlowd Method!) ");}

2015-03-08 17:12:33.240 Strong and weak[20017:738240] called the viewdidlowd Method!

2015-03-08 17:12:33.241 Strong and weak[20017:738240] objects have been destroyed!

If you change the __weak, then the statement executes, immediately frees the memory, add the breakpoint lesson proof

2015-03-08 17:15:41.480 Strong and weak[20062:739972] objects have been destroyed!

(LLDB)

See the stack diagram below

In the example above, the pointer variable Dashuai in the memory stack area, just the memory address, pointing to the memory in the heap area,

When the pointer no longer points to the memory, or the declaration period ends, the memory is freed

For a weak pointer, a dashed line

After execution, the object memory is immediately released.

Look between the controls again,

The view controller strongly references the child control View,view strongly.

[Self.view Addsubview]; // make view strong reference to control

So this is the time when the code is written, the control and code connected to the storyboard is always the default weak. Because that's when the view controller is referencing the child controls, and the view is strong, there's no need for the views controller to strong. Its memory-freeing process is:

If the program does not need to make strong references, then use if reference, analogy is linked list, one-stop, view and pose diagram, only a solid line connection (the parent class object, after the strongly referencing subclass object), the rest of the references are dashed with weak, because view is already strongly referenced by the child control, and the views controller can be used without strong. This is Apple's application memory management mechanism. Of course, using strong is not a must.

And the lazy loading mentioned earlier

Lazy loading of the control, is the manual write class properties of the set method, whether it has been loaded and other judgments, to avoid the process of repeated loading. Then the handwriting set, in the Viewdidload rewrite the code, will use the strong reference, because is the handwriting code, if still is weak, in the Viewdidload method, uses the control object, then once executes completes, immediately the memory is freed, because is weak. This way, the object disappears before the view is loaded.

Note the following method call time

Init Method-Initialize Program Viewdidload Method-Load View Viewwillappear method is called when the view of the Uiviewcontroller object is about to join the window;

The Viewdidapper method is called when the view of the Uiviewcontroller object has been added to the window;

Viewwilldisappear method Uiviewcontroller The view of the object is about to disappear, be overwritten, or be hidden;

The Viewdiddisappear method is called when the view of the Uiviewcontroller object has disappeared, is overwritten, or is hidden;

Didreceivememorywarning-Called when the deposit is insufficient

@property Parameter Usage Summary:

1> control with weak

2> Property object with strong

3> Non-object type with assign

4> string NSString with copy ( not a mutable string )

Tip: When you implement the interface layout in pure hand code, you need to use the strong strong pointer if you are using lazy loading to handle the interface control.

IOS Development note-strong and weak settings for the base UI (8) Control connection

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.