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

I don't know why it is not working, I think my code is right!

I'm pretty much sure that I've typed in the correct code but I'm unable to get through it!

The question says: ''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."

Please let me know the mistake in this code, if any.

data_collections.swift
var shoppingList = ["toothpaste", "bread", "eggs"]

var cart = shoppingList.removeAtIndex(1)

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Hammad,

As you said, the challenge asks you to use subscripting, which is the use of square brackets to index into an array. What you've done would work to get the item into the cart, but it's not what the challenge asks you to do, and it would also alter the shoppingList array, which you also aren't asked to do.

Here's how you would do it with subscripting:

var shoppingList = ["toothpaste", "bread", "eggs"]

var cart = shoppingList[1]

Whoa, I got it. It was so easy!

I just have to do this: var cart = shoppingList[1]