"Genesis Framework Foundations" was retired on June 9, 2017.

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

Subject(Swift) Can somebody please tell me how to append a for (name ) in to an array

Swift Loop (Range operator)

erik, can you give us a link to the challenge, or a copy of the code? Are you talking about doing the append inside a for loop?

can you tell me the syntax of that?

yeah that's what i mean

erik, sorry, but I don't know what you are referring to? syntax of what?

4 Answers

erik, I see I guessed wrong.

// Enter your code below
var results: [Int] = []

let multiplier = 6 //create constant

for i in 1...10 { //loop from 1 to 10 using a range
  results.append(i * multiplier)  //append the product of each number in the range and multiplier
}

Well, I'm guessing here, but this may be what you are interested in:

var nums = [Int]()
for n in 1...10 {
   nums.append(n)
}
nums  // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

First we create an array of Ints named nums. Then we loop through the range 1...10, and each time we append the value to the array.

thank you very much

but i did the same thing without the constant let multiplier = 6 and i did (for i in 1...10{ results.append(multiplier * 6)}

and it was right, is that ok?

erik, the editor can be really picky sometimes, and sometimes not. You are correct. I didn't read the directions carefully enough. They do say "For in loops also define a constant that temporarily stores the value in the iteration process. For the loop you're writing, name this constant multiplier." Interesting that the editor didn't complain.

I don't like hard-coding numbers in functions, loops, etc. So I created the separate constant by habit!