__block attribute on instance variable issue.

被刻印的时光 ゝ 提交于 2019-12-23 22:48:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!