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

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.