Swift for循环:用于索引,数组中的元素?

本文翻译自:Swift for loop: for index, element in array?

Is there a function that I can use to iterate over an array and have both index and element, like python's enumerate? 有没有可以用来遍历数组并具有索引和元素的函数,例如python的enumerate?

for index, element in enumerate(list):
    ...

#1楼

参考:https://stackoom.com/question/1cotB/Swift-for循环-用于索引-数组中的元素


#2楼

Yes. 是。 As of Swift 3.0, if you need the index for each element along with its value, you can use the enumerated() method to iterate over the array. 从Swift 3.0开始,如果需要每个元素的索引及其值,则可以使用enumerated()方法遍历数组。 It returns a sequence of pairs composed of the index and the value for each item in the array. 它返回由索引和数组中每个项目的值组成的对对的序列。 For example: 例如:

for (index, element) in list.enumerated() {
  print("Item \(index): \(element)"
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值