in Technology by
How to iterate a loop with index and element in Swift?

1 Answer

0 votes
by
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. 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)") } Before Swift 3.0 and after Swift 2.0, the function was called enumerate(): for (index, element) in list.enumerate() { print("Item \(index): \(element)") } Prior to Swift 2.0, enumerate was a global function. for (index, element) in enumerate(list) { println("Item \(index): \(element)") }

Related questions

0 votes
    in Obj-C it was possible to iterate through alphabet with: for (char x='A'; x...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have some data either in a list of lists or a list of tuples, like this: data = [[1,2,3], [4,5, ... store tuples or lists in my list? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    How to remove an element from a list by index in Python, I tried list.remove method but it search the list and ... How can I do this? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    which of the following tab should be used to add and delete element such as chapter number hyperlink etc a) ... style d) background Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    Which of these is a method of ListIterator used to obtain index of previous element? (a) previous() (b ... Framework of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Is there any shortcut method to find an index of an element in a vector, I have an element Q with me ... someone answer this doubt? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    Which of these is a method of ListIterator used to obtain index of previous element? (a) previous() ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I could swear I've seen the function (or method) that takes a list, like this [3, 7, 19] ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    HOW DO I SORT THIS BY BIRTHYEAR IN ASCENDING ORDER? I'm distance learning student and have no help. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    How would I iterate through these 5 array in a function call? Right now I am just manually iterating through each to form a ... gram address location unsigned char i; for( i= 0; i...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    This is a sample (edited slightly, but you get the idea) of my XML file: Test 192.168.1.1 Test ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    There is a DataFrame from pandas: import pandas as pd inp = [{'e2':20, 'e3':200}, {'e2':22,'e3':220}, { ... '] Can I do this in Pandas? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I am writing my first iOS application (iPhone only) with Swift. The main application view should allow user ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    What is the best way to iterate over a dictionary?...
asked Jan 15, 2021 in Technology by JackTerrance
...