Reputation: 1681
Swift 2.0 is throwing warnings
Immutable value 'a' was never used; Consider replacing it with '_' or removing it
Is it fine to replace the if-let with "_" ?
if let a = responseString {
//Code
}
Upvotes: 1
Views: 927
Reputation: 1
Put another way: (perhaps easier for new coders)
It is just saying that one of the variables was created but never used. So if you used var a somewhere else it would go away. It is also only a warning, which can be ignored if desired.
I had the same problem with my code and I just commented out the initialization of the variable since it wasn't being used currently. The solution that the compiler proposes (replacing with _) is inappropriate in cases where you want a variable of that name for future use.
Upvotes: 0
Reputation: 1205
It means that the code never accessed / used the constant "let a" inside the if- block.
Probably you don't need the if-block at all if you do not need to access "let a". Maybe you used responseString! instead? Or do you only want to execute your code if the responseString is not nil (a simple if responseString != nil would do it then)?
After the if-block, the "let a" a will be marked for garbage collection or marked to be deleted after the if block during compile time (not sure about that).
Thus the compiler thinks that it is a good idea to replace the condition, as the assignment
let a = responseString
will result in false if
responseString == nil
and true in all other cases.
This condition-assignment is used in 99% of the cases to assure that e.g. the nillable variable responseString is not nil or even more often to automatically unwrap it on-the-fly.
Upvotes: 2
Reputation: 131491
The syntax I've seen is
if let _ = responseString
{
//Code
}
That only runs the code inside the braces if responseString is not nil, but doesn't create a new constant.
Not sure what the compiler means by "Consider replacing it with ''". The Swift compiler's errors and warnings don't seem to make a lot of sense sometimes.
Upvotes: 1