Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,045 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 !