Xcode Swift 编译器警告永远不会执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35530568/
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
Xcode Swift compiler warning will never be executed
提问by Dribbler
I have code in this vein:
我有这样的代码:
enum enumThingType {
case apples
case oranges
case starfruit
}
func myFunc( enumThing: enumThingType ) -> String {
switch enumThing {
case .apples:
return "Hey, apples"
case .oranges:
return "Hey, oranges"
default:
return ""
}
}
I'm very slowly filling in the code for all of the cases, and I have need of the code to be functional while I code for starfruitand its ilk.
我正在非常缓慢地为所有情况填写代码,并且在我为杨桃及其同类代码编写代码时,我需要代码具有功能性。
All is good, but the compiler issues the warning will never be executedfor return ""
. It happily generates an executable, but I am compulsive and would like warnings and errors to go away.
一切都很好,但编译器发出警告,永远不会为return ""
. 它很高兴地生成了一个可执行文件,但我是强制性的,希望警告和错误消失。
If I delete the line return ""
, then I get the very real and compiler arresting Missing return in a function expected to return 'String'.
如果我删除该行return ""
,那么我会在预期返回 'String' 的函数中得到非常真实的编译器捕获Missing 返回。
Likewise, if I just delete default:
..., I get the equally arresting error Switch must be exhaustive, consider adding a default clause, which is fully understandable, but I am creative in my efforts to thwart warnings and errors.
同样,如果我只是删除default:
...,我会得到同样引人注目的错误Switch must be extinctive,考虑添加一个 default clause,这是完全可以理解的,但我在努力阻止警告和错误方面很有创意。
So given this construct, can I eliminate warnings and errors while still coding for existing cases for myFunc
?
因此,鉴于此构造,我是否可以消除警告和错误,同时仍然为 的现有案例编码myFunc
?
EDIT
编辑
As @appzYourLife pointed out, I omitted precompiler directives in my sample code that were creating the error. This code, with the precompiler directives, throws a warning:
正如@appzYourLife 指出的那样,我在示例代码中省略了导致错误的预编译器指令。此代码带有预编译器指令,会引发警告:
enum enumThingType {
case apples
case oranges
case starfruit
}
func myFunc( enumThing: enumThingType ) -> String {
switch enumThing {
case .apples:
return "Hey, apples"
case .oranges:
return "Hey, oranges"
default:
#if DEBUG
print( "Ouch!" )
#else
fatalError()
#endif
return ""
}
}
Because if the product is not created with DEBUG specified, then return can never happen--the FatalError()
prevents that.
因为如果产品不是在指定 DEBUG 的情况下创建的,那么返回永远不会发生——这会FatalError()
阻止这种情况。
This code, however, does notthrow a warning for all precompiler specified cases:
但是,此代码不会对所有预编译器指定的情况发出警告:
func myFunc( enumThing: enumThingType ) -> String {
switch enumThing {
case .apples:
return "Hey, apples"
case .oranges:
return "Hey, oranges"
default:
#if DEBUG
print( "Ouch!" )
return ""
#else
fatalError()
#endif
}
}
I just wanted to summarize the actual code fix for anyone who might gander across this, as the lack of a properly specified DEBUG flag fixed it for a DEBUG condition, but not for a non DEBUG condition. Thanks again for everyone who helped on this!
我只是想为任何可能对此感到困惑的人总结实际的代码修复,因为缺少正确指定的 DEBUG 标志将其修复为 DEBUG 条件,而不是非 DEBUG 条件。再次感谢所有为此提供帮助的人!
采纳答案by Luca Angeletti
I follow the chat you had with @matt, so it looks like the code you posted in your question was NOTthe real code ;)
我关注了你与@matt的聊天,所以看起来你在问题中发布的代码不是真正的代码;)
The real code is more something like this
真正的代码更像这样
func myFunc( enumThing: enumThingType ) -> String {
switch enumThing {
case .apples:
return "Hey, apples"
case .oranges:
return "Hey, oranges"
default:
#if DEBUG
print("...")
#else
fatalError()
#endif
return ""
}
}
Now I am getting your warning and it is totally correct.
现在我收到了你的警告,这是完全正确的。
In fact since DEBUG
is NOTdefined, the #else
block will be executed.
事实上,因为DEBUG
被NOT定义的,#else
块将被执行。
So the fatalError()
will be executed.
所以fatalError()
会被执行。
And since you are putting a return ""
immediately after a fatalError()
... well the return
of course will never be executed.
并且由于您在 areturn ""
之后立即放置了fatalError()
...return
当然永远不会被执行。
The compiler is right.
编译器是对的。
Update: defining the DEBUG complier flag
更新:定义 DEBUG 编译器标志
- Open the project root
- Select your target
- Look for the
Swift Compiler - Custom Flags
section - Add to debug or release this entry:
-D DEBUG
- 打开项目根目录
- 选择你的目标
- 寻找
Swift Compiler - Custom Flags
栏目 - 添加以调试或发布此条目:
-D DEBUG