Swift 2 unused constant warning

丶灬走出姿态 提交于 2020-01-12 23:16:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!