实例变量问题上的

实例变量问题上的

本文介绍了实例变量问题上的__block属性.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译Objective-C类时遇到以下错误:

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;
}
...

有什么解释为什么g ++认为我在非变量对象上应用了__block属性吗?

Is there any explanation why g++ thinks that I'm applying the __block attribute on a non-variable object ?

推荐答案

您不能在实例变量上使用__block,因为它完全没有必要.

You can't have __block on an instance variable as it is entirely unnecessary.

即,当您这样做时:

^{
     someIvar = ....;
 }();

该块捕获的是不变的,保留的对self的引用,并由此间接引用了iVar,因此,__block不执行任何操作,因为该变量既不是const复制的,也不是只读的.

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.

顺便说一句,这也是为什么在ARC下使用iVar时可能会出现循环引用"警告的原因.

Incidentally, this is also why, under ARC, you can end up with a "circular reference" warnings when using an iVar.

注意:在定义块语法时,我们试图考虑一种语法来表示这一点微妙之处,但是决定,除非有任何明显的(没有),否则改进了内存管理无需对ARC环境和/或LLVM静态分析器进行分析.

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.

这篇关于实例变量问题上的__block属性.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 16:34