问题
I have encountered the following error while compiling an Objective-C class:
VideoView.h:7: error: __block attribute can be specified on variables only
Also here is the important part of the header file:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface VideoView :UIView{
@private
__block AVPlayer *player;
}
...
Is there any explanation why g++ thinks that I'm applying the __block attribute on a non-variable object ?
回答1:
You can't have __block
on an instance variable as it is entirely unnecessary.
Namely, when you do:
^{
someIvar = ....;
}();
The block is capturing an immutable, retained, reference to self
and referring to the iVar indirectly through that and, thus, __block
does nothing as the variable is neither const-copied nor readonly.
Incidentally, this is also why, under ARC, you can end up with a "circular reference" warnings when using an iVar.
Note: We tried to think of a syntax for denoting this bit of subtlety when defining the blocks syntax, but decided that, barring anything obvious (which there wasn't), the improved memory management analysis of the ARC environment and/or the LLVM static analyzer made it unnecessary.
来源:https://stackoverflow.com/questions/10565431/block-attribute-on-instance-variable-issue