iOS Swift Collections and Control Flow Control Flow With Loops While and Repeat While

Philippe Duvin
Philippe Duvin
684 Points

Warning message with for loop

I wrote this simple code to say Hi! 5 times:

var hello = "Hi!" for a in 1...5 { print(hello) }

and got this warning I don't understand :

Immutable value "a" was never used; consider replacing it with '_' or removing it Thanks for the help

3 Answers

Steven Parker
Steven Parker
177,845 Points

It's only a warning, and the message is suggesting that since you don't use the value of "a" anywhere inside the loop, that you should substitute "_" (underscore) for "a" since it is designed specifically to act as a placeholder in cases where a variable or parameter will not be used.

Magnus Hållberg
Magnus Hållberg
16,953 Points

In your for loop “a” is a constant that holds the specific value of the range at each iteration of the loop. The warning suggest you should omit that constant since you are not using it. Hope this helps

Philippe Duvin
Philippe Duvin
684 Points

Thanks a lot guys ! That helps !