Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,275 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
17,232 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 !