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

Rodolfo Ocampo
Rodolfo Ocampo
2,663 Points

What I'm I missing? What is subscripting?

Hi team,

I have created a var name cart which removes the "bread" item from the list and assigned it to var cart. I believe it is doing the job but somehow i need to used "array subscripting". Any help?

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

var cart = shoppingList.removeAtIndex(1)

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Rodolfo,

Subscripting is the process of using type specific syntax to retrieve and/or modify data associated with a types instance, for this challenge the subscript syntax would be to use the square brackets which are built directly into the type Array and Dictionary.

What these brackets allow us to do is retrieve a value from our array or dictionary and use it somewhere else, in this case we need to retrieve the value for the second item in the array which is bread. We can get this value by using passing an integer to the subscript syntax and Swift will do the rest.

var cart = shoppingList[1]

Hope that helps.