提问人:thinker3 提问时间:6/4/2014 最后编辑:Vukašin Manojlovićthinker3 更新时间:9/7/2023 访问量:659071
如何在 Swift 中使用索引和元素迭代循环
How to iterate a loop with index and element in Swift
问:
有没有一个函数可以用来遍历数组并同时具有索引和元素,就像 Python 一样?enumerate
for index, element in enumerate(list):
...
答:
是的。从 Swift 3.0 开始,如果你需要每个元素的索引及其值,你可以使用 enumerated()
方法遍历数组。它返回由数组中每个项的索引和值组成的对序列。例如:
for (index, element) in list.enumerated() {
print("Item \(index): \(element)")
}
在 Swift 3.0 之前和 Swift 2.0 之后,该函数被称为:enumerate()
for (index, element) in list.enumerate() {
print("Item \(index): \(element)")
}
在 Swift 2.0 之前,是一个全局函数。enumerate
for (index, element) in enumerate(list) {
println("Item \(index): \(element)")
}
评论
enumerating
for (index, element) in
enumerated
for (offset, element) in
我在寻找一种使用字典做到这一点的方法时找到了这个答案,事实证明,调整它很容易,只需为元素传递一个元组即可。
// Swift 2
var list = ["a": 1, "b": 2]
for (index, (letter, value)) in list.enumerate() {
print("Item \(index): \(letter) \(value)")
}
从 Swift 2 开始,需要在集合上调用 enumerate 函数,如下所示:
for (index, element) in list.enumerate() {
print("Item \(index): \(element)")
}
Swift 5 为 . 具有以下声明:
Array
enumerated()
func enumerated() -> EnumeratedSequence<Array<Element>>
返回对序列 (n, x),其中 n 表示从零开始的连续整数,x 表示序列的一个元素。
在最简单的情况下,您可以与 for 循环一起使用。例如:enumerated()
let list = ["Car", "Bike", "Plane", "Boat"]
for (index, element) in list.enumerated() {
print(index, ":", element)
}
/*
prints:
0 : Car
1 : Bike
2 : Plane
3 : Boat
*/
但请注意,您不限于与 for 循环一起使用。事实上,如果您打算将 for 循环用于类似于以下代码的内容,那么您做错了:enumerated()
enumerated()
let list = [Int](1...5)
var arrayOfTuples = [(Int, Int)]()
for (index, element) in list.enumerated() {
arrayOfTuples += [(index, element)]
}
print(arrayOfTuples) // prints [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]
更快的方法是:
let list = [Int](1...5)
let arrayOfTuples = Array(list.enumerated())
print(arrayOfTuples) // prints [(offset: 0, element: 1), (offset: 1, element: 2), (offset: 2, element: 3), (offset: 3, element: 4), (offset: 4, element: 5)]
作为替代方案,您也可以与:enumerated()
map
let list = [Int](1...5)
let arrayOfDictionaries = list.enumerated().map { (a, b) in return [a : b] }
print(arrayOfDictionaries) // prints [[0: 1], [1: 2], [2: 3], [3: 4], [4: 5]]
此外,尽管它有一些限制,但可以很好地替代 for 循环:forEach
let list = [Int](1...5)
list.reversed().enumerated().forEach { print($0, ":", $1) }
/*
prints:
0 : 5
1 : 4
2 : 3
3 : 2
4 : 1
*/
通过使用 和 ,您甚至可以在 .例如:enumerated()
makeIterator()
Array
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
var generator = ["Car", "Bike", "Plane", "Boat"].enumerated().makeIterator()
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.setTitle("Tap", for: .normal)
button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
button.addTarget(self, action: #selector(iterate(_:)), for: .touchUpInside)
view.addSubview(button)
}
@objc func iterate(_ sender: UIButton) {
let tuple = generator.next()
print(String(describing: tuple))
}
}
PlaygroundPage.current.liveView = ViewController()
/*
Optional((offset: 0, element: "Car"))
Optional((offset: 1, element: "Bike"))
Optional((offset: 2, element: "Plane"))
Optional((offset: 3, element: "Boat"))
nil
nil
nil
*/
评论
enumerate
这是枚举循环的公式:
for (index, value) in shoppingList.enumerate() {
print("Item \(index + 1): \(value)")
}
有关更多详细信息,您可以查看 这里.
基本枚举
for (index, element) in arrayOfValues.enumerate() {
// do something useful
}
或者使用 Swift 3...
for (index, element) in arrayOfValues.enumerated() {
// do something useful
}
枚举、筛选和映射
但是,我最常将枚举与 map 或 filter 结合使用。例如,对几个数组进行操作。
在这个数组中,我想过滤奇数或偶数索引元素并将它们从 Ints 转换为 Doubles。因此,您可以获得索引和元素,然后过滤器检查索引,最后为了摆脱生成的元组,我将其映射到元素。enumerate()
let evens = arrayOfValues.enumerate().filter({
(index: Int, element: Int) -> Bool in
return index % 2 == 0
}).map({ (_: Int, element: Int) -> Double in
return Double(element)
})
let odds = arrayOfValues.enumerate().filter({
(index: Int, element: Int) -> Bool in
return index % 2 != 0
}).map({ (_: Int, element: Int) -> Double in
return Double(element)
})
使用有效,但不提供元素的真实索引;它只为每个连续的元素提供一个以 0 开头并递增 1 的 Int。这通常是无关紧要的,但在与类型一起使用时,可能会出现意外行为。采用以下代码:.enumerate()
ArraySlice
let a = ["a", "b", "c", "d", "e"]
a.indices //=> 0..<5
let aSlice = a[1..<4] //=> ArraySlice with ["b", "c", "d"]
aSlice.indices //=> 1..<4
var test = [Int: String]()
for (index, element) in aSlice.enumerate() {
test[index] = element
}
test //=> [0: "b", 1: "c", 2: "d"] // indices presented as 0..<3, but they are actually 1..<4
test[0] == aSlice[0] // ERROR: out of bounds
这是一个有点人为的例子,这在实践中并不是一个常见的问题,但我仍然认为知道这可能发生是值得的。
评论
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
是的,这就是它被称为枚举的原因。此外,slice 不是数组,因此它的行为不同也就不足为奇了。这里没有错误 - 一切都是设计使然。:)
filter
从 Swift 3 开始,它是
for (index, element) in list.enumerated() {
print("Item \(index): \(element)")
}
Xcode 8 和 Swift 3:
数组可以使用tempArray.enumerated()
例:
var someStrs = [String]()
someStrs.append("Apple")
someStrs.append("Amazon")
someStrs += ["Google"]
for (index, item) in someStrs.enumerated()
{
print("Value at index = \(index) is \(item)").
}
安慰:
Value at index = 0 is Apple
Value at index = 1 is Amazon
Value at index = 2 is Google
Swift 5.x:
我个人更喜欢使用这种方法:forEach
list.enumerated().forEach { (index, element) in
...
}
您也可以使用简短版本:
list.enumerated().forEach { print("index: \($0.0), value: \($0.1)") }
评论
您可以简单地使用枚举循环来获得所需的结果:
斯威夫特 2:
for (index, element) in elements.enumerate() {
print("\(index): \(element)")
}
斯威夫特 3 和 4:
for (index, element) in elements.enumerated() {
print("\(index): \(element)")
}
或者你可以简单地通过一个for循环来得到相同的结果:
for index in 0..<elements.count {
let element = elements[index]
print("\(index): \(element)")
}
希望它有所帮助。
对于那些想要使用 .forEach
斯威夫特 4
extension Array {
func forEachWithIndex(_ body: (Int, Element) throws -> Void) rethrows {
try zip((startIndex ..< endIndex), self).forEach(body)
}
}
或
array.enumerated().forEach { ... }
为了完整起见,您可以简单地遍历数组索引,并使用下标访问相应索引处的元素:
let list = [100,200,300,400,500]
for index in list.indices {
print("Element at:", index, " Value:", list[index])
}
使用 forEach
list.indices.forEach {
print("Element at:", $0, " Value:", list[$0])
}
使用收集方法。请注意,它返回一个元组集合,其中包含 和 :enumerated()
offset
element
for item in list.enumerated() {
print("Element at:", item.offset, " Value:", item.element)
}
使用 forEach:
list.enumerated().forEach {
print("Element at:", $0.offset, " Value:", $0.element)
}
那些将打印
元素位置:0 值:100
元素位置:1 值:200
元素位置:2 值:300
元素位置:3 值:400
元素位置:4 值:500
如果需要数组索引(而不是偏移量)及其元素,可以扩展 Collection 并创建自己的方法来获取索引元素:
extension Collection {
func indexedElements(body: ((index: Index, element: Element)) throws -> Void) rethrows {
var index = startIndex
for element in self {
try body((index,element))
formIndex(after: &index)
}
}
}
Alex 建议的另一种可能的实现是压缩包含其元素的集合索引:
extension Collection {
func indexedElements(body: ((index: Index, element: Element)) throws -> Void) rethrows {
for element in zip(indices, self) { try body(element) }
}
var indexedElements: Zip2Sequence<Indices, Self> { zip(indices, self) }
}
测试:
let list = ["100","200","300","400","500"]
// You can iterate the index and its elements using a closure
list.dropFirst(2).indexedElements {
print("Index:", $0.index, "Element:", $0.element)
}
// or using a for loop
for (index, element) in list.indexedElements {
print("Index:", index, "Element:", element)
}
这将 p[rint
索引:2 元素:300
索引:3 元素:400
索引:4 元素:500
索引:0 元素:100
索引:1 元素:200
索引:2 元素:300
索引:3 元素:400
索引:4 元素:500
评论
enumeratedIndices
zip(self.indices, self)
for element in zip(indices, self) { try body(element) }
indexedElements
for
zip(self.indices, self) .forEach(body)
forEach
@inlinable public func forEach( _ body: (Element) throws -> Void ) rethrows { for element in self { try body(element) } } }
我们调用枚举函数来实现这一点。喜欢
for (index, element) in array.enumerate() {
index is indexposition of array
element is element of array
}
对于您想要执行的操作,您应该在 Array 上使用以下方法:enumerated()
for (index, element) in list.enumerated() {
print("\(index) - \(element)")
}
评论
在函数式编程中使用 .enumerated():
list.enumerated().forEach { print($0.offset, $0.element) }
Swift 5.x:
let list = [0, 1, 2, 3, 4, 5]
list.enumerated().forEach { (index, value) in
print("index: \(index), value: \(value)")
}
或
list.enumerated().forEach {
print("index: \($0.offset), value: \($0.element)")
}
或
for (index, value) in list.enumerated() {
print("index: \(index), value: \(value)")
}
在 iOS 8.0/Swift 4.0 中+
您可以按照 Apple 文档使用:forEach
返回对序列 (n, x),其中 n 表示从零开始的连续整数,x 表示序列的一个元素。
let numberWords = ["one", "two", "three"]
numberWords.enumerated().forEach { (key, value) in
print("Key: \(key) - Value: \(value)")
}
如果您出于某种原因想要一个更传统的循环来访问数组中的元素,使用它们的索引:for
let xs = ["A", "B", "C", "D"]
for i in 0 ..< xs.count {
print("\(i) - \(xs[i])")
}
输出:
0 - A
1 - B
2 - C
3 - D
需要注意的是,苹果现在确实保证订单是“正确的”
https://developer.apple.com/documentation/swift/array/foreach(_:)
“以与for-in循环相同的顺序调用序列中每个元素的给定闭包”(Apple doco)。
.enumerated().forEach { print("\($0.offset) \($0.element)") }
评论