Remove item out of a every set | Learn Swift: Arrays & Sets project School Roster

So I want to remove Skyla from every class the student is in.

var classSet: Set = [spanish101, german101, advancedCalculus, artHistory, englishLiterature, computerScience]
for item in classSet{
if item.contains(“Skyla”) {
item.remove(“Skyla”)
}
}

error: cannot use mutating member on immutable value: ‘item’ is a ‘let’ constant

This method gives me an error, but why and how can I fix this?

When I print the classes, it gives me the correct classes:
for item in classSet{
if item.contains(“Skyla”) {
print(item)
}
}

Thanks!

@micro4024323500, welcome to the forums!

Looping through a set of sets and trying to alter one or more of the inner sets is not very straightforward in Swift, and I wouldn’t expect someone to figure it out based solely on this lesson.

For this particular challenge (removing Skyla from every class they are in), I believe the curriculum developers expected learners to individually use .remove("Skyla") on each of the classes she is in rather than by looping through classSet.

If you are still interested in seeing how to do this with a loop, you can check out my more in-depth explanation and repl.it example here:

Happy coding!

1 Like