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?

Jacob Schwartz
Jacob Schwartz
313 Points

Arrays Task 2 out of 2

I am having a little bit of trouble with the Arrays task 2 out of 2

I have the

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

good.

But, I don't know/remember how to put that into a numbering with println.

Thank you in advance!

arrays.swift
var todo = ["Learn Swift","Build App","Deploy App"]
println = ["Learn Swift\n t\ Build App\n \t Deploy App\n \t"]

18 Answers

Jacob Schwartz
Jacob Schwartz
313 Points

Steve Hunter

Okay, so, how should I apply it to my code to make it work?

Sorry for the delay - my internet has been down.

If the question is asking you to print out the nuber of items in the array, the above line is fine.

So your code will have the array definition,

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

then the output of the .count method,

println(todo.count)

If that doesn't work, there's more to the question that I'm not aware of.

Hope that helps!

Would, println("\(todo[0]") give you something you can work with?

Yes - I just tried that,

So, you created an array of three (0, 1, 2) strings and called it todo.

You then want to print this out. You've correctly called println() but your parameters need a little work.

An array counts from zero, so the first element in the array is zero. You access array elements by using your variable name, todo, followed by the element number within square brackets. So, your first array element is todo[0] which will give you ```"Learn Swift".

Inside the println() do the same ... println("(\todo[0])").

You can work with that to finish the exercise, I think, else give me a shout.

Steve.

Jacob Schwartz
Jacob Schwartz
313 Points

Steve Hunter, I tried doing that, I may have done it wrong, but I tried to follow your example.

Show me your code and I'll see what went wrong! :-)

Jacob Schwartz
Jacob Schwartz
313 Points

Steve Hunter

Here's the println: println("(\todo[0])", "(\todo[1])", "(\todo[0])")

The backslash comes before the bracket ...

println("\(todo[0])", "\(todo[1])", "\(todo[0])")

That should sort you out! :-)

Or better, println("\(todo[0])", "\(todo[1])", "\(todo[2])")

Jacob Schwartz
Jacob Schwartz
313 Points

Steve Hunter

Here is my current code:

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

println("(todo[0])" , "(todo[1])" , "(todo[2])")

Says in the Array I need to add the 'count.'

Also, thanks you so much for your time, just started learning, I don't mean to take up your time.

Right, the println() needs a backslash before the brackets, like:

println("\(todo[0])", "\(todo[1])", "\(todo[2])")

I'm not sure what you mean by "add the 'count'" but an array does have a property that can be accessed by dot-notation which will return the number of elements in the array. So, println(todo.count) will show '3' as there are three elements in that array.

I hope that helps, else let me know exactly what the question is asking.

Steve.

Jacob Schwartz
Jacob Schwartz
313 Points

Steve Hunter

The Error is saying this:

Bummer! You need to call the 'count' method on the array.

My code as it is right now:

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

println("(todo[0])", "(todo[1])", "(todo[2])")

And, I do believe that the todo.count needs to be added in, I'm just not sure where

Fine ... what is the question it is asking you? I don't know what it is asking you to do.

Jacob Schwartz
Jacob Schwartz
313 Points

Steve Hunter

The Question/Error it is asking me:

Bummer! You need to call the 'count' method on the array.

My code (the same):

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

println("(todo[0])", "(todo[1])", "(todo[2])")

The output Error:

swift_lint.swift:7:9: error: consecutive statements on a line must be separated by ';' var todo.count = ["Learn Swift","Build App","Deploy App"] ^ ; swift_lint.swift:7:9: error: expected expression var todo.count = ["Learn Swift","Build App","Deploy App"] ^ swift_lint.swift:7:5: error: type annotation missing in pattern var todo.count = ["Learn Swift","Build App","Deploy App"] ^

No. What is the question - you have given me the result of your answer. What are you trying to "pass"? What's at the top of the screen n the code challenge?

No. What is the question - you have given me the result of your answer. What are you trying to "pass"? What's at the top of the screen n the code challenge?

Jacob Schwartz
Jacob Schwartz
313 Points

Steve Hunter

Oh, sorry, the question at the top is

Print out the number of items in the todo array using println.

Jacob Schwartz
Jacob Schwartz
313 Points

Steve Hunter

Oh, sorry, the question at the top is

Print out the number of items in the todo array using println.

Right! So, we know that accessing the .count property tells us the number of items in the array.

Something like,

println(todo.count)