Swift 2未使用的持续警告 [英] Swift 2 unused constant warning
问题描述
我收到一条警告,说我的常量未被使用:
I am getting a warning saying my constant is unused:
永远不会使用不可变值'myConst'的初始化;考虑将作业替换为'_'或将其删除
Initialization of immutable value 'myConst' was never used; consider replacing with assignment to '_' or removing it
if someVal["value"] != nil {
let myConst = someVal["value"]
}
那么什么将重命名让myConst = someVal [value]
进入 _ myConst = someVal [value]
do / mean?
So what will renaming let myConst = someVal["value"]
into _ myConst = someVal["value"]
do/mean?
推荐答案
你不是用 _
替换let ,但是你要用它替换变量名。如果变量没有在代码中的任何地方使用,则它是无关紧要的,因此该行可以写成:
You're not replacing let with _
, but you're replacing the variable name with it. If the variable isn't used anywhere in the code it's irrelevant so the line can be written like:
_ = someVal["value"]
如果你想在某个地方使用它,你需要一个名字,以便以后引用它。但是当你不使用它时,写 _
要容易得多......
If you want to use it somewhere you need a name for it to reference it later on. But when you don't use it writing _
is a lot easier ...
这篇关于Swift 2未使用的持续警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!