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 Collections and Control Flow Introduction to Collections Adding Items to Arrays

2 error messages

When I do precisely as instructed in the video I still get 2 error messages from Xcode.

Line 15: "Cannot assign to immutable expression of type '[[String]]' "

Line 17: "Left side of mutating operator has immutable type '[[String]]' "

Here is my code:

// Collections and Control Flow

// Arrays

var todo: [String] = ["Finish collections course", "Buy groceries", "Respond to emails"]

// ADD NEW ITEM TO END OF ARRAY USING APPEND METHOD
todo.append("Pick up dry cleaning")

// CONCATENATING TWO ARRAYS

[1,2,3] + [4]

// CONCATENATE ARRAY CONTAINING STRING LITERAL TO TODO
[todo] = [todo] + ["Order book online"]
// PERFORMING THE SAME OPERATION USING THE UNARY ADDITION OPERATOR
[todo] += ["Order book online"]

Why am I getting these error messages? Do I need to do something about it or can I just continue?

On top of it all now I'm not getting any output from Xcode in the right hand pane.

What am I doing wrong? Am I doing something wrong or is this a bug from Xcode?

2 Answers

Pasan Premaratne
STAFF
Pasan Premaratne
Treehouse Teacher

Hey Linus,

This post is a bit old now but I just stumbled on this and wanted to explain. The "fix" is actually not Xcode related but an error in your code. It's a very subtle error in the following line:

[todo] = [todo] + ["Order book online"]

The line of code in the course was as follows:

todo = todo + ["Order book online"]

Notice the difference? In the course todo is written out just as the variable name whereas in your line of code you have it inside a set of square brackets like this [todo]. The variable todo is already an array. Remember that variables are just containers for what's stored in them. So todo refers to and thereby is an array of strings.

When you write out todo you are essentially writing out the array it stores. Now what you have done is put todo inside square brackets, which means you have created an array that stores an array.

The error you are getting is because you are adding an array containing a string literal ,["Order book online"], to an array containing an array containing string literals, [todo]!

If it helps clarify this [todo] is the same as writing [["Finish collections course", "Buy groceries", "Respond to emails"]]. Notice the two square brackets at the beginning and end which is not the same as having just one set of square brackets.

To fix this simply remove the square brackets that you have written around todo. So these two lines of code:

[todo] = [todo] + ["Order book online"]
[todo] += ["Order book online"]

Should be:

todo = todo + ["Order book online"]
todo += ["Order book online"]

Ok, Pasan! Thank you so much!

It's been a while like you said, so I'm going to have to go through these things soon in my own time, since I cannot remember fully this code task.

Cheers, Pasan! I'm very grateful =)

I'll answer this one myself actually...

I managed to find a simple workaround if anyone else is having this problem too.

The workaround that did it for me was to open a new playground file, choose a "blank" template, let it load until it says "Ready" in the Status Bar at the top, and then copy/paste the old project file into the new project file.

After all of this you simply erase the the old project file and put the new file in the old one's place with the old project files name (if you want to use the previous name that is.) Be a little bit careful here though: you should quit Xcode before moving around any files since Xcode will not find the new project file if it's still running.

After all of this go to the place where you store the new project file and double click/double tap the icon to open it.

I hope this can help any other frustrated students out there trying to learn how to develop for Apple devices =)