xcode iOS - 关于 setAlpha 的导入与前向声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10486810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:13:10  来源:igfitidea点击:

iOS - import vs forward declaration regarding setAlpha

iosxcodeclassimportforward-declaration

提问by Romes

I'm trying to do some animations on an object that I have setup via IB. I'm adding a forward declaration to my .h like so:

我正在尝试在我通过 IB 设置的对象上做一些动画。我正在向我的 .h 添加一个前向声明,如下所示:

@class MySpecialClass;

and then setup a property like so:

然后像这样设置一个属性:

@property (nonatomic, retain) IBOutlet MySpecialClass *specialClass;

I want to be able to hide the specialClassusing setAlpha, but I get the following error from xcode when trying to compile.

我希望能够隐藏specialClassusing setAlpha,但是在尝试编译时从 xcode 收到以下错误。

Receiver type 'MySpecialClass' for instance message is a forward declaration.

Do I need to import my class instead of a forward declaration? I'd like to not have to import anything unnecessary if I don't have to.

我需要导入我的类而不是前向声明吗?如果不需要,我不想导入任何不必要的东西。

回答by JoeCortopassi

Forward declaration just tells the compiler, "Hey, I know I'm declaring stuff that you don't recognize, but when I say @MyClass, I promise that I will #import it in the implementation".

前向声明只是告诉编译器,“嘿,我知道我在声明你不认识的东西,但是当我说@MyClass 时,我保证我会在实现中#import”。

You use forward declaration to prevent stuff like circular includes (MyClass imports YourClass which imports MyClass which...), but the 'promise' you make with your code, is that you'll #import it later

您使用前向声明来防止诸如循环包含之类的东西(MyClass 导入 YourClass 导入 MyClass 其中...),但是您对代码做出的“承诺”是您稍后会#import

回答by graver

You need to import it. Forward declaration is just to silence the compiler that this class exists, but it has no idea about its members, methods, properties, size...

你需要导入它。前向声明只是让编译器沉默这个类的存在,但它不知道它的成员、方法、属性、大小......