- (void)testBlockWeakObj1
{
UILabel *tl = [[UILabelalloc]init];//本地局部变量
__weak UILabel *weakTL = tl;//弱引用变量
NSLog(@"%s tl %p",__FUNCTION__ , &tl);
NSLog(@"%s tl %@",__FUNCTION__ , tl);
NSLog(@"%s weakTL %p",__FUNCTION__ , &weakTL);
NSLog(@"%s weakTL %@",__FUNCTION__ , weakTL);
void (^TestBlock)(void) = ^{
//复制tl指针,并且强引用tl
NSLog(@"%s tl %p",__FUNCTION__ , &tl);
NSLog(@"%s tl %@",__FUNCTION__ , tl);
//复制weakTL指针,并且弱引用weakTL
NSLog(@"%s weakTL %p",__FUNCTION__ , &weakTL);
NSLog(@"%s weakTL %@",__FUNCTION__ , weakTL);
};
tl = nil;
TestBlock();
NSLog(@"block is %@", TestBlock);
}
/*输出结果
2014-02-21 17:50:42.591 TestBlock[13888:70b] -[ViewController testBlockWeakObj1] tl 0xbfffc9b0
2014-02-21 17:50:42.591 TestBlock[13888:70b] -[ViewController testBlockWeakObj1] tl <UILabel: 0x8c57c30; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8c562d0>>
2014-02-21 17:50:42.592 TestBlock[13888:70b] -[ViewController testBlockWeakObj1] weakTL 0xbfffc9ac
2014-02-21 17:50:42.592 TestBlock[13888:70b] -[ViewController testBlockWeakObj1] weakTL <UILabel: 0x8c57c30; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8c562d0>>
2014-02-21 17:50:42.592 TestBlock[13888:70b] __35-[ViewController testBlockWeakObj1]_block_invoke tl 0x8b09434
2014-02-21 17:50:42.593 TestBlock[13888:70b] __35-[ViewController testBlockWeakObj1]_block_invoke tl <UILabel: 0x8c57c30; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8c562d0>>
2014-02-21 17:50:42.611 TestBlock[13888:70b] __35-[ViewController testBlockWeakObj1]_block_invoke weakTL 0x8b09438
2014-02-21 17:50:42.612 TestBlock[13888:70b] __35-[ViewController testBlockWeakObj1]_block_invoke weakTL <UILabel: 0x8c57c30; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8c562d0>>
2014-02-21 17:50:42.612 TestBlock[13888:70b] block is <__NSMallocBlock__: 0x8b09420>
*/
- (void)testBlockWeakObj2
{
__block UILabel *tl = [[UILabelalloc]init];//加上block关键字就变成全局变量
__weak UILabel *weakTL = tl;//弱引用变量
NSLog(@"%s tl %p",__FUNCTION__ , &tl);
NSLog(@"%s tl %@",__FUNCTION__ , tl);
NSLog(@"%s weakTL %p",__FUNCTION__ , &weakTL);
NSLog(@"%s weakTL %@",__FUNCTION__ , weakTL);
void (^TestBlock)(void) = ^{
//tl是全局变量,强引用
NSLog(@"%s tl %p",__FUNCTION__ , &tl);
NSLog(@"%s tl %@",__FUNCTION__ , tl);
//复制weakTL指针,并且弱引用weakTL
NSLog(@"%s weakTL %p",__FUNCTION__ , &weakTL);
NSLog(@"%s weakTL %@",__FUNCTION__ , weakTL);
};
tl = nil;
TestBlock();
NSLog(@"block is %@", TestBlock);
}
/*输出结果
2014-02-21 17:55:13.832 TestBlock[13926:70b] -[ViewController testBlockWeakObj1] tl 0xbfffc9b0
2014-02-21 17:55:13.832 TestBlock[13926:70b] -[ViewController testBlockWeakObj1] tl <UILabel: 0x8a32470; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8a37410>>
2014-02-21 17:55:13.832 TestBlock[13926:70b] -[ViewController testBlockWeakObj1] weakTL 0xbfffc994
2014-02-21 17:55:13.833 TestBlock[13926:70b] -[ViewController testBlockWeakObj1] weakTL <UILabel: 0x8a32470; frame = (0 0; 0 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x8a37410>>
2014-02-21 17:55:13.833 TestBlock[13926:70b] __35-[ViewController testBlockWeakObj1]_block_invoke tl 0x8a3bdf8
2014-02-21 17:55:13.833 TestBlock[13926:70b] __35-[ViewController testBlockWeakObj1]_block_invoke tl (null)
2014-02-21 17:55:13.834 TestBlock[13926:70b] __35-[ViewController testBlockWeakObj1]_block_invoke weakTL 0x8a3bdd8
2014-02-21 17:55:13.834 TestBlock[13926:70b] __35-[ViewController testBlockWeakObj1]_block_invoke weakTL (null)
2014-02-21 17:55:13.834 TestBlock[13926:70b] block is <__NSMallocBlock__: 0x8a3bdc0>
*/