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

Why is this code not acceptable?

The challenge asks for the creation of a shopping list array. I keep receiving an error saying I need to create an array named "shoppingList". Here's my code:

let shoppingList: [String] = ["toothpaste", "bread", "eggs"]

This code works without an issue in xcode. I also tried declaring the array without explicitly specifying the String type. What am i doing wrong?

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Jacob,

The challenge only wants you to create a variable not a constant which you've done.

var shoppingList: [String] = ["toothpaste", "bread", "eggs"]
Holt Hunter
Holt Hunter
4,629 Points

Have you just tried this:

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

I think you should leave out the ```[String]``` because it isn't a String but an Array.  Also I don't think you need the colon after ```var shoppingList```, but I may be wrong.
Chris Shaw
Chris Shaw
26,676 Points

I think you should leave out the [String] because it isn't a String but an Array. Also I don't think you need the colon after let shoppingList, but I may be wrong.

The type [String] denotes an array of the type String, in Swift arrays can only be one type so you can explicitly declare the type or let the compiler infer the type based on the values, if you need values of different types you can use an NSArray type which doesn't infer one type but AnyObject.

Alternatively you can also just use [AnyObject] but you would need to typecast and unpack the data before you can use it as AnyObject is an optional type.