http://stackoverflow.com/questions/3975675/objective-c-blocks-is-there-a-way-to-avoid-self-being-retained
从以上的讨论中,我没没有办法让block引用参数而不导致selfretain的方法,但是提到了三种方式来实现在block中引用参数而不导致self retain的解决方案:
1 将当前对象当做block的参数传进去,其形式:
[request getRaidersFrompageNum:pageNum finished:^(id target,id result, ServerResponseState state)
{
[(RaidersViewController *)target getRaidersInfoAndShow:state response:result pageNum:pageNum];
}];
- (void)blockTimer
{
__block TestViewController *blockSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
blockSelf->_timer=[NSTimer scheduledTimerWithTimeInterval:1.0
target:blockSelf
selector:@selector(caculateLeftTimeForTomorrow)
userInfo:nil
repeats:YES] ;
[[NSRunLoop currentRunLoop] addTimer:blockSelf->_timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
}
将self手动转化为__block类型,这样即可使block不在retain self