1.参数block
当做参数的block ,如果参数没有引起到其他的值的话,是全局的block,但当引用到了值的话,不管是self 还是weakSelf ,都是栈block。这要和前面的中写的 《arc 中block 知多少?》的一文中有所区别
2.demo
//
// ViewController.m
// 当做参数的局部变量的block
//
// Created by apple on 16/7/6.
// Copyright © 2016年 李重阳. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) void(^block)();
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray * arr = @[@"1",@"2"];
__weak typeof(self) weakSelf = self;
[self addBlock:^(int a) {//__NSGlobalBlock__
NSLog(@"block0 = %d",a);
}];
[self addBlock:^(int a) {//__NSStackBlock__
NSLog(@"block2(%@) = %d",weakSelf,a);
}];
[self addBlock:^(int a) {//__NSStackBlock__
NSLog(@"block3(%@) = %d",self,a);
}];
NSLog(@"selfblock = %@",self.block);//__NSMallocBlock__
}
- (void)addBlock:(void(^)(int))block {
self.block = block;
block(1);
NSLog(@"block = %@",block);
}
@end
3. 总结
block 当做参数的时候,如果只是在当前函数中调用,可以不用copy 或者strong 拷贝到堆中例如 在- (void)addBlock:(void(^)(int))block,只是在当前的函数中调用,如果这个block 在其他地方调用就必须 @property (nonatomic,strong) void(^block)() 来指向当前的block ,拷贝到堆中,防止提前释放。