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 Swift 2.0 Collections and Control Flow Introduction to Collections Working with Arrays

Swift Arrays

Challenge Task 1 of 4

We just learned the ins and outs of arrays, so let's put that knowledge into practice. Start by declaring a new array of Ints with six numbers as values. Assign this array to a variable named arrayOfInts.

Hard

arrays.swift
// Enter your code below

var arrayOfInts: [String] = [ "1", "2", "3", "4", "5", "6" ]

9 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Ary:

I have not done this challenge, but from what I see it's asking you, you should create an array of Ints, and not of Strings.

It should look like this:

var arrayOfInts = [1,2,3,4,5,6]

You were very close, in fact that's an acceptable array, but for this challenge it has to be of the Type Int.

Good luck.

We just learned the ins and outs of arrays, so let's put that knowledge into practice. Start by declaring a new array of Ints with six numbers as values. Assign this array to a variable named arrayOfInts.

var arrayOfInts = [1,2,3,4,5,6,]

Now that we have an array declared, since it is a mutable array, we can add and remove items. Let's start by adding two more values to our existing array. There are two ways we can add items to an array and I want you to give both a try. Add one item to the array using the append method.

Remember: We invoke methods on an array using a period or dot. After the dot we write out the method name and a value to append in parentheses following the name.

Add another item to the array by concatenating an array. When concatenating, assign the results of the expression back to arrayOfInts

var arrayOfInts = [1,2,3,4,5,6,] arrayOfInts.append(7) arrayOfInts = arrayOfInts + [8]

We also learned about reading values from an array. Retrieve the 5th item (remember array indexes start at 0) and assign the result to a constant named value.

var arrayOfInts = [1,2,3,4,5,6,] arrayOfInts.append(7) arrayOfInts = arrayOfInts + [8] let value = todo[4]

// code assigned to a constant named value // answer below let value = arrayOfInts[4]

Great job so far! Clearly you know how to add and read items from the array so in this challenge let's try getting rid of a value.

For this task, remove the 6th item from the array and assign the result to a constant named discardedValue.

To remove an item, use the method removeAtIndex() and put the index number in between parentheses that you want to remove.

// answer let discardedValue = arrayOfInts.removeAtIndex(5)

var arrayOfInts: [Int] = [0,1,2,3,4,5] arrayOfInts.append(6) arrayOfInts.append(7) arrayOfInts += [8]

var arrayOfInts: [Int] = [0,1,2,3,4,5] arrayOfInts.append(6) arrayOfInts.append(7) arrayOfInts = arrayOfInts + [8]

Here it is, var arrayOfInts: [Int] = [0,1,2,3,4,5] arrayOfInts.append(6) arrayOfInts = arrayOfInts + [7]

var arrayOfInts = [1,2,3,4,5,6]