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 Swift Basics (retired) Collections What is an Array?

Arjun Parashar
PLUS
Arjun Parashar
Courses Plus Student 2,150 Points

What am I doing wrong here? I can't figure out my mistake.

I am not able to figure out my mistake and I am getting a bummer. Help me out.

arrays.swift
var todo = ["Learn Swift","Build App","Deploy App"]
Julien riera
Julien riera
14,665 Points

I would have written :

var todo: [String] = ["Learn Swift", "Build App", "Deploy App"]

But doesn't work either, while it's what I usualy do on Xcode.

3 Answers

David Lin
David Lin
35,864 Points

What you have looks correct. My hunch is there's something wrong with Treehouse's grading system.

Tobias Meier
Tobias Meier
6,017 Points

var todo: [String]

tells Swift that your variable todo is an array of strings.

var todo = ["Learn Swift","Build App","Deploy App"]

tells Swift that todo is an array with the strings "Learn Swift","Build App" and "Deploy App" as its elements aka an array of strings.

Back when I perused teamtreehouse on a very intensive daily basis, I used to run into this kind of problem quite a lot.

Culprit is how the backend compares your solution to the "right" one.

I played around a lot and usually the checks are result based. Sometimes there seem to be shortcuts in place which throw these kind of errors.

Don't let this dishearten you! Take it as a challenge to tinker with your code to find "their" solution as this often gives you insights you otherwise would not achieve!

hth and a pleasant weekend to all!

yep, swift will automatically generate an Array

Tobias Meier
Tobias Meier
6,017 Points

Y .. No!

Swift will generate an array - and of a certain type - if it is presented with one.

e.g.:

var myArray = ["my first string entry in my array","my second string entry in my array"]

If you do:

var myArray = {"my bad":"don't know better"}

Take a guess what happens.