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

Ricardo Gonzalez
Ricardo Gonzalez
2,286 Points

How am I suppose to remove a string from the array and then assign it to a variable?

Need help on to do this challenge.

data_collections.swift
var shoppingList = ["toothpaste","bread","eggs"]
var cart = shoppingList.removeAtIndex(1)
Stepan Ulyanin
Stepan Ulyanin
11,318 Points

Seems fine to me, you will remove bread and assign it to the cart variable, what is the problem?

3 Answers

This is a possible solution. You would first want to store the array value you want to a variable and then remove it from the array.

var shoppingList = ["toothpaste","bread","eggs"]
//Stores the the item from array in removedItem variable
var cart = shoppingList[1]
//Removes "bread" from the array
shoppingList.removeAtIndex(1)

//The array will now look like this: ["toothpaste", "eggs"]
Stepan Ulyanin
Stepan Ulyanin
11,318 Points

Well removeAtIndex() method returns the the item that is being deleted from the array so there should be no need to assign to the variable prior to removing

YouΒ΄re right, but it helps doing it this way for him to understand better.

It seems like you are going above and beyond the original question, which is why your functionally correct code is being marked as incorrect. The original Code Challenge asks you to create a variable named "cart", and assign it the value of the String "bread" using subscripting syntax, but does not mention removing it from the original array. So the code that the challenge is looking for is

var shoppingList = ["toothpaste","bread","eggs"]
var cart = shoppingList[1]
Ricardo Gonzalez
Ricardo Gonzalez
2,286 Points

Ok I see. Thanks You'all for the help.