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 Build a Simple iPhone App with Swift Creating a Data Model Creating a Data Collection

Philip Wilkinson
Philip Wilkinson
2,273 Points

Code doesn't compile in the challenge

In the Data Collection's challenge - the code I run works fine in Xcode / playground but won't compile in the challenge. Keeps saying "use subscripting to access the array element", which I am...

var shoppingList = ["toothpaste", "bread", "eggs"]
var cart = shoppingList.removeAtIndex(1)

4 Answers

Sebastian Röder
Sebastian Röder
13,878 Points

Your are using the removeAtIndex() method to solve the challenge but are expected to use the subscript notation instead of a method call. That's the one were you give the index in square brackets [ ], remember?

One difference between your solution and subscript notation is that you remove the element from the array while the array will be unchanged if you access the element with a subscript.

Gard Mikael Fjeldavli
Gard Mikael Fjeldavli
19,416 Points

Try:

var shoppingList = ["toothpaste", "bread", "eggs"]
var cart = shoppingList[1]
Philip Wilkinson
Philip Wilkinson
2,273 Points

Well, it said "remove an item from the list and add it to the variable cart" - so your solution works.. just the question is phrased wrong :-)

Gard Mikael Fjeldavli
Gard Mikael Fjeldavli
19,416 Points

In that case, I understand if you are confused. The text is now:

"We are at the grocery store and we picked up the bread. Using array subscripting, retrieve the string "bread" from the array and assign it to a variable named cart."

...which makes more sense! =)

Philip Wilkinson
Philip Wilkinson
2,273 Points

Great point Seb - it didn't say a method call. Suppose I thought that if you put an item in your shopping cart then it can be removed from the list - :-)