帮朋友理解 Block 和 循环引用,写了几个测试,欢迎大家也测一下。
1 、全局变量中的 Block 属性包含了 self 。
@interface Object1 : NSObject
@property (nonatomic, copy) void(^block)();
@end
@interface ViewController ()
@property (nonatomic, strong) Object1 *obj;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.obj.block = ^ {
NSLog(@"%@", self);
};
}
@end
2 、全局变量中的 Block 属性包含了 self 的全局变量。
@interface Object1 : NSObject
@property (nonatomic, copy) void(^block)();
@end
@interface Object2 : NSObject
@end
@interface ViewController ()
@property (nonatomic, strong) Object1 *obj;
@property (nonatomic, strong) Object2 *obj2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.obj.block = ^ {
NSLog(@"%@", _obj2);
};
}
@end
3 、全局变量中的 Block 属性包含了 self 的局部变量。
@interface Object1 : NSObject
@property (nonatomic, copy) void(^block)();
@end
@interface ViewController ()
@property (nonatomic, strong) Object1 *obj;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *string = @"";
self.obj.block = ^ {
NSLog(@"%@", string);
};
}
@end
4 、全局变量中的方法中的 Block 参数包含了 self 。
@interface Object1 : NSObject
- (void)foo:(void(^)())param;
@end
@interface ViewController ()
@property (nonatomic, strong) Object1 *obj;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.obj foo:^{
NSLog(@"%@", self);
}];
}
@end
5 、全局变量中的方法中的 Block 参数包含了 self 的全局变量。
@interface Object1 : NSObject
- (void)foo:(void(^)())param;
@end
@interface Object2 : NSObject
@end
@interface ViewController ()
@property (nonatomic, strong) Object1 *obj;
@property (nonatomic, strong) Object2 *obj2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.obj foo:^{
NSLog(@"%@", _obj2);
}];
}
@end
6 、全局变量中的方法中的 Block 参数包含了 self 的局部变量。
@interface Object1 : NSObject
- (void)foo:(void(^)())param;
@end
@interface ViewController ()
@property (nonatomic, strong) Object1 *obj;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *string = @"";
[self.obj foo:^{
NSLog(@"%@", string);
}];
}
@end
7 、 self 的 Block 属性包含了 self 。
@interface ViewController ()
@property (nonatomic, copy) void(^block)();
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.block = ^ {
NSLog(@"%@", self);
};
}
@end
8 、 self 的 Block 属性包含了 self 的全局变量。
@interface ViewController ()
@property (nonatomic, copy) void(^block)();
@property (nonatomic, strong) NSString *string;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.block = ^ {
NSLog(@"%@", _string);
};
}
@end
9 、 self 的 Block 属性包含了 self 的局部变量。
@interface ViewController ()
@property (nonatomic, copy) void(^block)();
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *string = @"";
self.block = ^ {
NSLog(@"%@", string);
};
}
@end
10 、 self 的 Block 本地变量包含了 self 。
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
void(^block)() = ^ {
NSLog(@"%@", self);
};
}
@end
11 、 self 的 Block 本地变量包含了 self 的全局变量。
@interface ViewController ()
@property (nonatomic, strong) NSString *string;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
void(^block)() = ^ {
NSLog(@"%@", _string);
};
}
@end
12 、 self 的实例方法中的 Block 参数包含了 self 。
@interface ViewController ()
@end
@implementation ViewController
- (void)foo:(void(^)())param {}
- (void)viewDidLoad {
[super viewDidLoad];
[self foo:^{
NSLog(@"%@", self);
}];
}
@end
13 、 self 的实例方法中的 Block 参数包含了 self 的全局变量。
@interface ViewController ()
@property (nonatomic, strong) NSString *string;
@end
@implementation ViewController
- (void)foo:(void(^)())param {}
- (void)viewDidLoad {
[super viewDidLoad];
[self foo:^{
NSLog(@"%@", _string);
}];
}
@end
14 、 self 的实例方法中的 Block 参数包含了 self 的局部变量。
@interface ViewController ()
@end
@implementation ViewController
- (void)foo:(void(^)())param {}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *string = @"";
[self foo:^{
NSLog(@"%@", string);
}];
}
@end
15 、 self 的类方法中的 Block 参数包含了 self 。
@interface ViewController ()
@end
@implementation ViewController
+ (void)foo:(void(^)())param {}
- (void)viewDidLoad {
[super viewDidLoad];
[[self class] foo:^{
NSLog(@"%@", self);
}];
}
@end
16 、 self 的类方法中的 Block 参数包含了 self 的全局变量。
@interface ViewController ()
@property (nonatomic, copy) NSString *string;
@end
@implementation ViewController
+ (void)foo:(void(^)())param {}
- (void)viewDidLoad {
[super viewDidLoad];
[[self class] foo:^{
NSLog(@"%@", _string);
}];
}
@end
17 、 self 的类方法中的 Block 参数包含了 self 的局部变量。
@interface ViewController ()
@end
@implementation ViewController
+ (void)foo:(void(^)())param {}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *string = @"";
[[self class] foo:^{
NSLog(@"%@", string);
}];
}
@end
18 、其他类中的类方法中的 Block 参数包含了 self 。
@interface Object1 : NSObject
+ (void)foo:(void(^)())param;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[Object1 foo:^{
NSLog(@"%@", self);
}];
}
@end
19 、局部对象的方法中的 Block 参数包含了 self 。
@interface Object1 : NSObject
- (void)foo:(void(^)())param;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[Object1 new] foo:^{
NSLog(@"%@", self);
}];
}
@end