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

Nathan Noye
Nathan Noye
4,370 Points

Selecting a certain item in a For In loop

Hey fam,

I'm trying to build an app where it lists 5 or so things and outputs them to a label. Only issue is following the DRY method, I had to use a For In loop so I wouldn't be rewriting a bunch of code. My only issue is when I output this to a Label, my label on displays the bottom item. Is there a way I can make it so it displays everything as a list? for example this is what is should look like

  1. eggs
  2. bread
  3. milk

but it appears as this

  1. milk

Thanks!

anything helps!

Nathan Noye
Nathan Noye
4,370 Points

Note - I need it to appear as a list in a single label

1 Answer

Hint: string concatenation, then assign the concatenated string to the label after the loop has run.

Nathan Noye
Nathan Noye
4,370 Points

Thats what I had done initially

lblWork.text = "(randWork)"

only thing is instead of coming out at

  1. milk
  2. eggs 3.butter

it came out as 3.butter

it only showed the last item. Any way of making it show all in a list form or sending each item to it's own label?

Note - my code is correct for concatenation but the browser keeps changing it on my side so I'm not sure if the forward slash is appearing or not.