Array warning: "Immutable value was never used"

I created a preinitialized array and attempted to iterate over the array.

var someValues = Array(repeating: 9, count: 10)


for eachElement in someValues {
    print("This is the number nine!")
}

This results in "This is the number nine!" being printed ten times to the console. However, I also get a warning in Xcode that says the following:

Immutable value 'eachElement' was never used; consider replacing with '_' or removing it.

Can someone please explain to me why this message keeps showing up? I even replaced each the name eachElement in my for in loop with simply i and I still get the same warning in Xcode. Does anyone know why this is? Thank you for helping.

In the for loop, you enumerate through all values in the Array. In your syntax, you have set the name of the current element variable to be eachElement. So, in the first run of the loop, eachElement is set to the first element. In the second run of the loop, eachElement is set to the second element. This continues until all values have been enumerated through (or the loop is stopped partway through, with a control flow statement like return or continue)

So, if you had an array like [1, 2, 3, 4] and code like the following:

var someValues = [1, 2, 3, 4]

for eachElement in someValues {
    print(eachElement)
}

... then in the console, you would see the output as:

1
2
3
4

My code would not cause the warning because the eachElement value is actually being used inside of the loop block. The value is read and printed to the console.

In your case, eachElement is not referenced inside of the loop. You simply print the same static message "This is the number nine!" each time. You will end up with ten of these being printed to the console as your array holds ten elements, but it will be repeating the same message ten times.

As you are not using the value of eachElement, Swift warns you that it is being named for no reason. As hinted in the second half of the warning message, you can silence the warning by replacing eachElement with _. The underscore in Swift means 'I intentionally do not want to name something as I don't need to reference it'.

for _ in someValues {
    print("This is the number nine!")
}

This will compile without any warnings or errors, and the result will be identical to your original formulation.

However, it is not really the ideal way to write the code. Your print statement implicitly assumes that the someValues array only holds nines. If you changed someValues to be equal to [1, 2, 3], the print statement would still say "This is the number nine!". You need to make the body of the loop block dynamic based on the value of the element. For instance, changing it to print("This is the number \(eachElement)!") using values [1, 2, 3] would result in console output:

This is the number 1!
This is the number 2!
This is the number 3!

Obviously, you could expand the logic to use if conditional statements or whatever else you need to do to accomplish the desired result.

If you really do want to print the same static message nine times, then you don't need to declare an array at all. You can use a range. The expression 0..<9 means make a range of 9 elements (the values 0, 1, 2, 3, 4, 5, 6, 7, 8). If you use this as the subject of the for loop, you are effectively saying to do the thing nine times over. If you don't care about the index at any point, you can again use the underscore. So that would look like:

for _ in 0..<9 {
    print("This is the number nine!")
}

This results in the same output as your original code but using a more idiomatic form that better signifies intent (and also produces no warnings).

Hopefully, that all makes sense.

5 Likes

Thank you so much for this explanation, I get it now! Thank you! :)

Terms of Service

Privacy Policy

Cookie Policy