-
-
-
One, Parent-child mutual hold, delegate mode
| |
@interface ftappcentermainviewcontroller () { } &NBSP; < Code class= "Java Color1" > @property (weak,nonatomic) uitableview* Mytableview; @end |
-
-
The Mytableview in this use the weak modifier.
| 1 |
@property(nonatomic, weak) id<ftactionsheetdelegate>delegate; |
"Recommended Method":
Child only the object of the parent is the weak type:
| 1 |
@property(nonatomic, weak) id<ftactionsheetdelegate>delegate; |
second, block
"Case":
Look at the following code:
?
| 2 " Span style= "FONT-SIZE:14PT;" >3 5 |
< Span style= "FONT-SIZE:14PT;" >typedef void (^ Requestnavicallback) (Nsinteger navicode,nsinteger httpcode,nserror * error); @interface Ftnavimanager:nsobject { " Span style= "FONT-SIZE:14PT;" > @property (nonatomic, strong) requestnavicallback navicallback; |
This is a request navigation class, the class attribute holds the Requestnavicallback, at this time, if the requestnavicallback again holds self, inevitably causes the circular reference.
"Recommended Method":
If there is a circular reference, the compiler prompts for a warning.
If the object does not hold a block object, a circular reference is not generated. If the object holds a block object, it is defined when the block references self:
?
| 1 |
__weak typeof(self) weakSelf = self; |
Third, Nstimer
"Case":
?
| 1 2 3 4 5 6 |
@interfaceFtKeepAlive : NSObject{ NSTimer* _keepAliveTimer; // 发送心跳timer}//实现文件_keepAliveTimer = [NSTimer scheduledTimerWithTimeInterval:_expired target:self selector:@selector(keepLiveStart) userInfo:nil repeats:YES]; |
The class holds the _keepalivetimer,_keepalivetimer and holds the self, causing a circular reference.
"Recommended Method":
Nstimer will hold the object, so: Before deleting the object, the timer's Invalidate method needs to be set.
?
| 1 2 3 4 |
-(void)stopKeepAlive{ [_keepAliveTimer invalidate]; _keepAliveTimer = nil;} |
Three scenarios in which IOS can easily be cited as "circular references"