问题
I am getting a warning saying my constant is unused:
Initialization of immutable value 'myConst' was never used; consider replacing with assignment to '_' or removing it
if someVal["value"] != nil {
let myConst = someVal["value"]
}
So what will renaming let myConst = someVal["value"]
into _ myConst = someVal["value"]
do/mean?
回答1:
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 ...
回答2:
we can use Wildcard Pattern ' _ ' for unused constant warings
回答3:
If you are never using it there is no need for the compiler to use let/var variable_name
.
So do not replace it with _
if you mean to use it later.
回答4:
I think that this suggestion was planning for 'if let' statements and similar. Probably will be removed for useless statements on future updates.
来源:https://stackoverflow.com/questions/32705070/swift-2-unused-constant-warning