1.參數(shù)block
當(dāng)做參數(shù)的block 抵碟,如果參數(shù)沒(méi)有引起到其他的值的話豹储,是全局的block贷盲,但當(dāng)引用到了值的話,不管是self 還是weakSelf 剥扣,都是棧block巩剖。這要和前面的中寫(xiě)的 《arc 中block 知多少?》的一文中有所區(qū)別
2.demo
//
// ViewController.m
// 當(dāng)做參數(shù)的局部變量的block
//
// Created by apple on 16/7/6.
// Copyright ? 2016年 李重陽(yáng). 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. 總結(jié)
block 當(dāng)做參數(shù)的時(shí)候朦乏,如果只是在當(dāng)前函數(shù)中調(diào)用球及,可以不用copy 或者strong 拷貝到堆中例如 在- (void)addBlock:(void(^)(int))block,只是在當(dāng)前的函數(shù)中調(diào)用呻疹,如果這個(gè)block 在其他地方調(diào)用就必須 @property (nonatomic,strong) void(^block)() 來(lái)指向當(dāng)前的block 吃引,拷貝到堆中,防止提前釋放刽锤。