Jak iterować pętlę z indeksem i elementem w języku Swift
Czy istnieje funkcja, której mogę użyć do iteracji tablicy i mieć zarówno indeks, jak i element, jak w Pythonie enumerate
?
for index, element in enumerate(list):
...
15 answers
Tak. Począwszy od Swift 3.0, jeśli potrzebujesz indeksu dla każdego elementu wraz z jego wartością, możesz użyć enumerated()
metoda do iteracji nad tablicą. Zwraca sekwencję par składających się z indeksu i wartości dla każdej pozycji w tablicy. Na przykład:
for (index, element) in list.enumerated() {
print("Item \(index): \(element)")
}
Przed Swift 3.0 i po Swift 2.0 funkcja została wywołana enumerate()
:
for (index, element) in list.enumerate() {
print("Item \(index): \(element)")
}
Przed Swift 2.0, enumerate
była funkcją globalną.
for (index, element) in enumerate(list) {
println("Item \(index): \(element)")
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-27 16:56:29
Swift 5 dostarcza metodę zwaną enumerated()
dla Array
. enumerated()
ma następującą deklarację:
func enumerated() -> EnumeratedSequence<Array<Element>>
Zwraca sekwencję par (n, x), gdzie n reprezentuje kolejną liczbę całkowitą zaczynającą się od zera, A x element ciągu.
W najprostszych przypadkach można użyć enumerated()
z pętlą for. Na przykład:
let list = ["Car", "Bike", "Plane", "Boat"]
for (index, element) in list.enumerated() {
print(index, ":", element)
}
/*
prints:
0 : Car
1 : Bike
2 : Plane
3 : Boat
*/
Zauważ jednak, że nie jesteś ograniczony do używania enumerated()
z pętlą for. W rzeczywistości, jeśli planujesz użyć enumerated()
z pętlą for dla czegoś podobnego do poniższego kodu, robisz to źle:
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)]
Szybszym sposobem na to jest:
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)]
Jako alternatywę można również użyć enumerated()
z 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]]
Ponadto, chociaż ma pewne ograniczenia, forEach
może być dobrym zamiennikiem dla pętli for:
let list = [Int](1...5)
list.reversed().enumerated().forEach { print($0, ":", $1) }
/*
prints:
0 : 5
1 : 4
2 : 3
3 : 2
4 : 1
*/
Używając enumerated()
i makeIterator()
, możesz nawet ręcznie iterować na swoim Array
. Na przykład:
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
*/
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-07-10 14:51:10
Począwszy od Swift 2, funkcja enumerate musi być wywołana na zbiorze w następujący sposób:
for (index, element) in list.enumerate() {
print("Item \(index): \(element)")
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-23 13:48:28
Znalazłem tę odpowiedź szukając sposobu, aby to zrobić za pomocą słownika , i okazuje się, że jest to dość łatwe do dostosowania, wystarczy podać krotkę dla elementu.
// Swift 2
var list = ["a": 1, "b": 2]
for (index, (letter, value)) in list.enumerate() {
print("Item \(index): \(letter) \(value)")
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-04-18 05:32:51
Możesz po prostu użyć pętli wyliczenia, aby uzyskać pożądany wynik:
Swift 2:
for (index, element) in elements.enumerate() {
print("\(index): \(element)")
}
Swift 3 & 4:
for (index, element) in elements.enumerated() {
print("\(index): \(element)")
}
Lub możesz po prostu przejść przez pętlę for, aby uzyskać ten sam wynik:
for index in 0..<elements.count {
let element = elements[index]
print("\(index): \(element)")
}
Mam nadzieję, że to pomoże.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-12 08:52:50
Dla kompletności możesz po prostu iterować nad indeksami tablicy i użyć indeksu dolnego, aby uzyskać dostęp do elementu o odpowiednim indeksie:
let list = [100,200,300,400,500]
for index in list.indices {
print("Element at:", index, " Value:", list[index])
}
Using forEach
list.indices.forEach {
print("Element at:", $0, " Value:", list[$0])
}
Za pomocą metody collection enumerated()
. Zwróć uwagę, że zwraca zbiór krotek z offset
i element
:
for item in list.enumerated() {
print("Element at:", item.offset, " Value:", item.element)
}
Użycie forEach:
list.enumerated().forEach {
print("Element at:", $0.offset, " Value:", $0.element)
}
Będą drukować
Element at: 0 Value: 100
Element at: 1 Value: 200
Element at: 2 Value: 300
Element at: 3 Value: 400
Element at: 4 Value: 500
Jeśli potrzebujesz indeksu tablicy (nie offsetu) i jej elementu, możesz rozszerzyć kolekcję i utworzyć własną metodę, aby uzyskać indeksowane elementy:
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)
}
}
}
Inną możliwą implementacją sugerowaną przez Alexa jest zapięcie indeksów kolekcji z jej elementami:
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) }
}
Testowanie:
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)
}
TO P [rint
Indeks: 2 Element: 300
Index: 3 Element: 400
Index: 4 Element: 500
Index: 0 Element: 100
Index: 1 Element: 200
Index: 2 Element: 300
Index: 3 Element: 400
Index: 4 Element: 500
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-07-20 19:53:05
Wyliczenie podstawowe
for (index, element) in arrayOfValues.enumerate() {
// do something useful
}
Lub z Swift 3...
for (index, element) in arrayOfValues.enumerated() {
// do something useful
}
Wylicz, filtruj i Mapuj
Jednak najczęściej używam enumerate w połączeniu z mapą lub filtrem. Na przykład z operowaniem na kilku tablicach.
W tej tablicy chciałem filtrować nieparzyste lub parzyste zindeksowane elementy i przekonwertować je z Ints na Doubles. Więc enumerate()
pobiera indeks i element, następnie filtr sprawdza indeks, a na koniec, aby pozbyć się wynikowej krotki, mapuję go do element.
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)
})
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-25 14:07:11
Użycie .enumerate()
działa, ale nie dostarcza prawdziwego indeksu elementu; dostarcza tylko Int rozpoczynający się od 0 i zwiększający się o 1 dla każdego następnego elementu. Zwykle jest to nieistotne, ale istnieje możliwość nieoczekiwanego zachowania, gdy jest używane z typem ArraySlice
. Wpisz następujący kod:
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
Jest to nieco wymyślony przykład, i nie jest to częsty problem w praktyce, ale mimo to myślę, że warto wiedzieć, że może się to zdarzyć.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-31 21:46:27
Począwszy od Swift 3, to
for (index, element) in list.enumerated() {
print("Item \(index): \(element)")
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-20 12:03:30
Oto wzór pętli wyliczenia:
for (index, value) in shoppingList.enumerate() {
print("Item \(index + 1): \(value)")
}
Aby uzyskać więcej szczegółów, możesz sprawdzić tutaj .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-23 14:27:39
Swift 5.x:
Ja osobiście wolę używać metody forEach
:
list.enumerated().forEach { (index, element) in
...
}
Możesz również użyć skróconej wersji:
list.enumerated().forEach { print("index: \($0.0), value: \($0.1)") }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-01-20 14:07:32
Dla tych, którzy chcą używać forEach
.
Swift 4
extension Array {
func forEachWithIndex(_ body: (Int, Element) throws -> Void) rethrows {
try zip((startIndex ..< endIndex), self).forEach(body)
}
}
Lub
array.enumerated().forEach { ... }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-16 07:43:17
Xcode 8 i Swift 3:
Tablica może być wyliczana za pomocą tempArray.enumerated()
Przykład:
var someStrs = [String]()
someStrs.append("Apple")
someStrs.append("Amazon")
someStrs += ["Google"]
for (index, item) in someStrs.enumerated()
{
print("Value at index = \(index) is \(item)").
}
Konsola:
Value at index = 0 is Apple
Value at index = 1 is Amazon
Value at index = 2 is Google
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-04-24 13:45:59
Do tego, co chcesz zrobić, powinieneś użyć metody enumerated()
na tablicy :
for (index, element) in list.enumerated() {
print("\(index) - \(element)")
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-05-20 15:46:47
Wywołaliśmy funkcję enumerate, aby to zaimplementować. jak
for (index, element) in array.enumerate() {
index is indexposition of array
element is element of array
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2021-01-02 20:31:44