标签云

微信群

扫码加入我们

WeChat QR Code

Is there a function that I can use to iterate over an array and have both index and element, like python's enumerate?for index, element in enumerate(list):...


Although it seems like a tuple, in Swift 1.2 - not sure about 2.0 - enumerate returns an EnumerateSequence<base: SequenceType> struct.

2019年05月23日18分12秒

Does this add a performance overhead?

2019年05月24日18分12秒

Leviathlon noticeable or measurable performance overhead that will matter? No.

2019年05月23日18分12秒

Maybe they'll change to the gerund, enumerating for Swift 4. Exciting!

2019年05月24日18分12秒

Honey for (index, element) in when using enumerated is misleading. should be for (offset, element) in

2019年05月24日18分12秒

Is getting access to the index the only benefit of using enumerate?

2019年05月24日18分12秒

it does not actually provide the true index of the element; it only provides an Int beginning with 0 and incrementing by 1 for each successive element Yes, that's why it's called enumerate. Also, slice is not array, so no surprise it behaves differently. There's no bug here - everything is by design. :)

2019年05月24日18分12秒

True, but I never called it a bug. It just is potentially unexpected behavior that I thought was worth mentioning for those who didn't know how it could interact negatively with the ArraySlice type.

2019年05月24日18分12秒

Are you aware of any way to get the index of the actual element - for example if using filter first?

2019年05月23日18分12秒