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

adding Arrays

I'm having a bit of a tough time on the arrays some tips Is much appreciated

arrays.swift
// Enter your code below
var arrayOfInts = [1,2,3,4,5,6]
todo.append 

2 Answers

Anish Walawalkar
Anish Walawalkar
8,534 Points

You're almost there. There are two ways in which you can add an element to an array (not of fixed size):

  1. using the apend(value) function. e.g. name.append("John")
  2. you can also just directly append to an array using the + operator. e.g. names += ["John"]

so in code:

// Enter your code below
var arrayOfInts: [Int] = [1,2,3,4,5,6]
arrayOfInts.append(7)
arrayOfInts += [8]

Hello, incase you are still looking for the answer:

//You did array declation right
var arrayOfInts = [1,2,3,4,5,6]

//To append a vlaue into an array, use the following syntax as mentioned by Anish
arrayOfInts.append(7)

//You can also add a value to an array by concatenating the array with another array
arrayOfInts += [8]

//We refer a specific value in an array by their index number.
let value = arrayOfInts[4]

//And finally, we use the removeAtIndex() method to delete a value from an array, again refer to that value by its index number
let discardedValue = arrayOfInts.removeAtIndex(5)