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 2.0 Collections and Control Flow Control Flow With Loops For In Loops

Vidur Bahl
PLUS
Vidur Bahl
Courses Plus Student 1,440 Points

how insert all numbers in this array ??

how to write codes for inserting values in a array ??

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

for multiplier in 1...10{

    results =  [multiplier * 6]



}

2 Answers

Stone Preston
Stone Preston
42,016 Points

Currently you are assigning an array to results. So each time the loop runs the value of results is just overwritten with a new array value. Thats not want you want. You want to APPEND a new element onto the array, not assign it a new value each time

you need to append the result onto the array using either the append method, or the += operator:

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

for multiplier in 1...10{

    results +=  [multiplier * 6]
}

or

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

for multiplier in 1...10{

    results.append(multiplier * 6)



}

for more information on arrays see the swift documentation

Vidur Bahl
PLUS
Vidur Bahl
Courses Plus Student 1,440 Points

hello stone,

But with this code too array does not include array of [ 6,12,18,24,30....till 60]

it only shows 60....how to show all values in array using loop function ??