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 Modifying an Array

todo.append

my answer for this is : todo.append ("Debug App","Fix Bugs"), I'm not sure which part is wrong... I followed what the video taught, but the result is wrong.

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

2 Answers

Stone Preston
Stone Preston
42,016 Points

the documentation on the append method looks like this

append(_:)

Adds a new item as the last element in an existing array.

the append method takes one argument. you could append an array containing both items like so:

var todo = ["Learn Swift", "Build App", "Deploy App"]
todo.append (["Debug App","Fix Bugs"])

you could also use +=

var todo = ["Learn Swift", "Build App", "Deploy App"]
todo += ["Debug App","Fix Bugs"]

or append them separately

using +=:

var todo = ["Learn Swift", "Build App", "Deploy App"]
todo += "Debug App"
todo += "Fix Bugs"

using append:

var todo = ["Learn Swift", "Build App", "Deploy App"]
todo.append("Debug App")
todo.append("FixBugs")

Thank you very much! So I need to use [] if there are more than one items, () if there is one item, right?

Stone Preston
Stone Preston
42,016 Points

yes if you want to append more than one item to an array, you need to append an array of items (an array literal has [ ] with items inside it). if its just one item, you dont need to append an array, you can append an item directly

Thank you very much for your help!

Chris Shaw
Chris Shaw
26,676 Points

Hi xiaohu li,

There are two ways you can append new items to an Array, the first is the append function which only accepts one argument of the same type, e.g. an array or string.

todo.append("Debug App")
todo.append("Fix Bugs")

The second method is the shortcut which uses the += operator to concatenate the arrays together.

todo += ["Debug App", "Fix Bugs"]

Happy coding!

Thank you very much! That helped me a lot!