How to see index - Swift

New to coding so teaching myself through Swift and the Intro to App Development with Swift book.

Whilst learning about Arrays and Loops, I am unable to see the index of an array in a list? How do I do this?

Note: The exercise says you can use the Show Result Button in the results sidebar and see the indices in the inline view.

Any help is appreciated, plus any tips for coding as I'm new and looking to create a number of apps, including games.

Thanks - Harrison.
Answered by Claude31 in 631066022
Not sure of whet you want exactly to do. You should show the code on which your question is based.

But, if you have an array, you can loop through elements and their index:

Code Block
let myArray = ["a", "b", "c", "d"]
for (index, item) in myArray.enumerated() {
print("index", index, "item", item)
}

And get
index 0 item a
index 1 item b
index 2 item c
index 3 item d


Accepted Answer
Not sure of whet you want exactly to do. You should show the code on which your question is based.

But, if you have an array, you can loop through elements and their index:

Code Block
let myArray = ["a", "b", "c", "d"]
for (index, item) in myArray.enumerated() {
print("index", index, "item", item)
}

And get
index 0 item a
index 1 item b
index 2 item c
index 3 item d


Thank you so much for your quick response!

It seems like the instructions I am following in the exercise are outdated now compared to the version of Swift Playgrounds I am using.

Thanks for the tip with showing the code, will remember for future use. Happy Hunting!
How to see index - Swift
 
 
Q