It is true that the most basic knowledge of the understanding is not thorough, today look at the iOS two modifiers: __block and __weak. It's also a refresher.
1, first say weak,< weak citation >
We know that the use of weak, such as declaring a control property, will use weak.
Look at the code:
@property (nonatomic,weak) UILabel *label;
Of course this is not to say that the declaration control must use weak, but instead I manually create the control that most will use strong. But if you are more careful, you will find that when we use xib, we use the weak attribute. The question is, when is the time when we use strong and weak? Let's keep looking down.
Create a control from storyboard or xib
When the control is placed on the view, the following reference relationship has been formed, taking UILabel as an example:uiviewcontroller->uiview->subview-> UILabel
Then you declare a weak attribute for this Uilabel
@property (nonatomic,weak) ibooutlet UILabel * label;
The equivalent of Xib is a strong reference to this label, and the property you declare is a weak reference to it.
Manually create the control a). Declaring a control as strong
@property (Nonatomic,strong) UIlabel * label;
So you just have to implement this control:
_label = [[UIlabel alloc]init];[ Self.view Addsubview:_label]
b). Declare the control as weak
@property (nonatomic,weak) UIlabel * label;
So you need to implement this control:
UIlabel *label == label;[ Self.view ADDSUBVIEW:_BTN];
Questions about the Block circular reference
Because block replicates external variables, it causes circular references if you are not aware of them. In this case, you need to turn the referenced party into weak, thus avoiding circular references.
Look at the code:
Blocksviewcontroller *BLOCKVC =*weekself = self ; /* * * 1, use weak reference to solve block circular reference problem * * 2, block can intercept automatic variables, and can not be modified in block blocks (unless with __block modifier) The value of WeekSelf.textField.text here is modified , and the __block modifier is not used. The reason is because TextField is a global variable, if you define a local variable, such as: the definition of "text" can not be modified, the compiler will error,*///block callback pass value [ BLOCKVC returnvalue:^ (NSString *= content; // text = content; // this will suggest missing __block;}];
2. The difference between strong and weak
A) Strong: a strong reference, which we usually refer to, the survival of the object directly determines the survival of the point. If there is no reference to an object and the object no longer appears in the list, the object is freed from memory.
b) Weak: Weak reference, does not determine the survival of the object. Even if an object is held with countless weak references, it is cleared if no strong references point to it.
c) Strong and copy are used to modify the properties of strong references, weak to modify the properties of weak references;
D) Strong, weak need arc support to use, copy does not require ARC support
e) strong and retain function similar, weak and assign similar, only when the object disappears after the weak will automatically turn the pointer to nil;
3, about __block
Look at the code first:
0 ; void (^block) (void) = ^1;}; Block (); NSLog (@ "val =%ld", Value);}
We may not be able to draw any conclusions now, so let's look at the following code, in order to be intuitive, I'm presenting it in the form of a picture:
Now the contrast is clear.
Here's a summary:
a) The 1.__block object can be modified and re-assigned in the Block .
b) The 2.__block object is not strongly referenced by block in block, so there is no circular reference problem.
c) 3.blocks can access local variables, but cannot be modified.
If you modify a local variable, you need to add __block, so __block is the value that lets you modify the external variable.
For variable references outside the block, the block defaults to copying it into its data structure for access.
For external variable references modified with __block, the block is a copy of its reference address for access.
4. The difference between __block and __weak modifiers:
1>.__block can be used in either ARC or MRC mode, can be decorated with objects, and can also be decorated with basic data types.
2>.__weak can only be used in Arc mode, can only be decorated with objects (nsstring), and cannot be decorated with basic data types (int).
The 3>.__block object can be re-assigned in the block, __weak not.
* * Link: * *
[Talk about the realization of Objective-c block]
[Blocks is Objective-c ' s]
[iOS Development-Learn block]
(IOS) __block and __weak know