0

Say we have

struct item {
  var val = 0
}

var items = [item]()
items.append(item(val: 0))
items.append(item(val: 1))
items.append(item(val: 2))

This code is obviously not possible since i is a let constant:

for i in items {
  i.val += 10
}

What works is this kinda of verbose loop:

for i in 0..<items.count {
  items[i].val += 10
}

and this map:

items = items.map { item(val:$0.val + 10) }

Both solutions don't look very "swifty" to me. Any ideas how to make them look better?

Also, I wonder what is the most performant way to accomplish this task (assuming items is a very large array)?

cocoapriest
  • 1,869
  • 3
  • 22
  • 36

0 Answers0