大多数记录在案的块使用示例都演示了使用简单变量的闭包,但我对任何访问周围代码中存在的对象的尝试感到困惑。例如,这会以一种丑陋、无益的方式崩溃:
@interface VisualizerPreset : EyeCandyPreset {
float changeSourceRate;
float (^frontPanelSlider2DisplayValueBlock)(void);
}
....
VisualizerPreset *it;
it = [[VisualizerPreset alloc] init];
it.changeSourceRate = 0.4;
it.frontPanelSlider2DisplayValueBlock = ^(void) {
return it.changeSourceRate;
};
....
// this crashes
NSLog(@"%f",it.frontPanelSlider2DisplayValueBlock());