xcode 未调用 [super 方法] 时发出警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16094095/
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
Give warning when [super method] is not called
提问by jordanperry
When not using ARC, you get a warning when not calling [super dealloc] in your dealloc method.
不使用 ARC 时,如果不在 dealloc 方法中调用 [super dealloc],则会收到警告。
I'm trying to implement something similar with a class that gets subclassed often to warn the person implementing the subclass when they don't call super.
我试图用一个经常被子类化的类来实现类似的东西,以警告实现子类的人,当他们不调用 super 时。
Any ideas?
有任何想法吗?
回答by bbum
Recent versions of llvm have added an attribute that indicates that subclasses must call super:
最近版本的 llvm 添加了一个属性,指示子类必须调用 super:
@interface Barn:NSObject
- (void)openDoor NS_REQUIRES_SUPER;
@end
@implementation Barn
- (void) openDoor
{
;
}
@end
@interface HorseBarn:Barn
@end
@implementation HorseBarn
- (void) openDoor
{
;
}
@end
Compiling the above produces the warning:
编译上述内容会产生警告:
Method possibly missing a [super openDoor] call
回答by rob mayoff
UPDATE
更新
See bbum's answerfor a direct way to tell the compiler that subclassers must call super
.
见bbum的回答了直接的方式告诉编译器子类必须调用super
。
ORIGINAL
原来的
What Apple does generally is to provide a hook, like viewDidLoad
. Apple doesn't do any work in the base -[UIViewController viewDidLoad]
. It's an empty method. Instead, Apple does its work elsewhere, in some private method that you're not allowed to override, and calls your viewDidLoad
from that method. So even if you forget to call [super viewDidLoad]
in your UIViewController
subclass, all of Apple's work still gets done.
Apple 通常做的是提供一个钩子,比如viewDidLoad
. Apple 在 base 中不做任何工作-[UIViewController viewDidLoad]
。这是一个空方法。取而代之的是,Apple 在其他地方工作,在某些不允许覆盖的私有方法中,并viewDidLoad
从该方法调用您的方法。因此,即使您忘记调用[super viewDidLoad]
您的UIViewController
子类,Apple 的所有工作仍然可以完成。
Perhaps you can revise your API to use a similar pattern.
也许您可以修改您的 API 以使用类似的模式。
回答by berbie
NS_REQUIRES_SUPER
is the modern expression for
是现代表达
__attribute__((objc_requires_super))