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 Introduction to Collections Working with Arrays

Wang Lei
Wang Lei
2,766 Points

Could you please help me on this coding challenge?

I have tried on X-code, the system doesn't allow adding Int item through + operator: for example: arrayOfInts=arrayOfInts+(8) I have tried insert Int item and it works.

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

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

I have not looked at the challenge, but from what I see you are trying to add more ints into the array.

Here are some ways you can do this:

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

// If you want to add ONE int use this method.
arrayOfInts.append(7)

// If you want to add ONE int at an specific index, use this method.
arrayOfInts.insert (8, atIndex: 7)

// If you want to add MORE than ONE int, do it this way. 
// It will add the ints to the end of the array.
arrayOfInts += [9,10]

Good luck :)

Wang Lei
Wang Lei
2,766 Points

Thank you very much. problem solved. It seems I use the wrong brackets.