在 Xcode 6 中关闭指定初始值设定项检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25429685/
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
Turn off designated initializer checking in Xcode 6
提问by Clay Bridges
I'm getting the compile error:
我收到编译错误:
error: convenience initializer missing a 'self' call to another initializer [-Werror,-Wobjc-designated-initializers]
Compile-checked designated initializers might be a good thing, but if I don't want deal with that right now, how can I turn this off?
编译检查指定初始值设定项可能是一件好事,但如果我现在不想处理它,我该如何关闭它?
回答by bandejapaisa
Following on from Clay's answer..
继克莱的回答之后..
Method 3
方法三
You might want to suppress the warning on one occurrence, not all of them:
您可能希望在一次而不是所有情况下取消警告:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-designated-initializers"
- (instancetype) initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
// do whatever I was doing....
}
return self;
}
#pragma clang diagnostic pop
EDIT: However, I've only actually used this once myself. I find it the same amount (or a little more) effort just to do it properly if it's a single case. So flag up your constructor with NS_DESIGNATED_INITIALIZER. And if it then complains about the init method not being overridden add an init method to your header with NS_UNAVAILABLE.
编辑:但是,我自己实际上只使用过一次。如果它是一个单独的案例,我发现它只是为了正确地做到这一点而付出了同样多(或更多)的努力。所以用 NS_DESIGNATED_INITIALIZER 标记你的构造函数。如果它然后抱怨 init 方法没有被覆盖,请使用 NS_UNAVAILABLE 在您的标头中添加一个 init 方法。
回答by Clay Bridges
Method 1
方法一
In your project:
在您的项目中:
- Edit the build settings for your target (?-1, select project, or cf. Apple docs).
- Search for "Other warning flags".
- Add
-Wno-objc-designated-initializers
.
- 编辑目标的构建设置(?-1,选择项目,或参见Apple 文档)。
- 搜索“其他警告标志”。
- 添加
-Wno-objc-designated-initializers
.
You can also do some combination of this and -Wobjc-designated-initializers
on a per file basis or with clang diagnostic pushes and pops (cf. @bandejapaisa's "Method 3" answerbelow).
您还可以-Wobjc-designated-initializers
在每个文件的基础上或使用 clang 诊断推送和弹出来执行此操作的一些组合(参见下面@bandejapaisa 的“方法 3”答案)。
Method 2
方法二
This method allows you to switch back and forth between Xcode 5 & 6, and also provides you a reminder to fix the designated initializer stuff.
此方法允许您在 Xcode 5 和 6 之间来回切换,并提醒您修复指定的初始化程序。
For iOS development, put this in your .pch
(precompiled header) file:
对于 iOS 开发,把它放在你的.pch
(预编译头)文件中:
#ifdef __IPHONE_8_0
// suppress these errors until we are ready to handle them
#pragma message "Ignoring designated initializer warnings"
#pragma clang diagnostic ignored "-Wobjc-designated-initializers"
#else
// temporarily define an empty NS_DESIGNATED_INITIALIZER so we can use now,
// will be ready for iOS8 SDK
#define NS_DESIGNATED_INITIALIZER
#endif
The analogto __IPHONE_8_0
for OS X 10.10 is __MAC_10_10
.
该模拟到__IPHONE_8_0
为OS X 10.10是__MAC_10_10
。
Why?
为什么?
If you are interested in why these messages exist, you can check out this SO answeror these Apple docs.