Fred Clausen Fred Clausen - 1 year ago 114
Swift Question

Iterate an array with index in Swift 3

I'm trying to iterate an array with an index in Swift 3 but keep getting

Expression type '[Int]' is ambiguous without more context

this is reproducible with the following example in a playground:

var a = [Int]()
// Gives above error
for (index, value) in a {
print("\(index): \(value)")

I'm not sure what context it is asking for.

Answer Source

You forgot to call a.enumerated(), which is what gives you the (index, value) tuples. for value in a is what gives you each element without the index.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download