2016-01-13 67 views
0

在下面的代码片段中,有三种类型的块。我的问题是哪个对象引用每个块? (启用ARC)谁保留区块?

typedef void (^CompletedBlock)(void); 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [UIView animateWithDuration:<#(NSTimeInterval)#> 
          delay:<#(NSTimeInterval)#> 
         options:<#(UIViewAnimationOptions)#> 
        animations:^{<#code#>} 
        completion:^(BOOL finished) {<#block 1#>}]; 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
^{<#block 2#>}); 

    [self myMethod:^{<#block 3#>}]; 

} 

- (void)myMethod:(CompletedBlock)completed 
{ 
    completed(); 
} 

回答

2

animateWithDuration:delay:options:animations:completion:的情况下,它会立即运行animations块。在方法返回后,没有对象存储对该块的强引用。它将completion块存储在未指定的地方。也许它保留了当前的CATransaction

dispatch_async的情况下,队列保留该块。

myMethod:的情况下,没有对象存储对该块的强(保留)引用。在通话期间,对该块的强引用会被存储在堆栈中。