-2

I can't seem to find much online, but how can I get the first (and then second and third, later on) entry of an array?

My array is being saved to UserDefaults elsewhere and then pulled for use here.

Thanks!

Noah Evans
  • 309
  • 3
  • 12
  • `yourArray[0]` or `yourArray.first` is first element of an array. `yourArray[1]` is second element of an array. What is your issue? Please describe more. – emrcftci Jun 11 '20 at 10:56
  • [Apple doc for Array](https://developer.apple.com/documentation/swift/array), [Swift Programming Language chapter on Array](https://docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html#ID107). It's not that hard to find information – Joakim Danielson Jun 11 '20 at 11:29

2 Answers2

2

Did you mean to loop through the elements? You can use for in or forEach for that.

var array: [MyType]
for element in array {
    print(element)
}
array.forEach { element in
    print(element)
}
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
2

Maybe you are trying to iterate value from continually with index also

Here is two way you can get first index value than second index value third index value with index also . hope you will get your result .

First way :

var stringArray = ["a","b","C","D","a","i","x","D"]

for (index, element) in stringArray.enumerated() {

       print("Item \(index): \(element)")

     }

Another way :


for index in 0 ..< stringArray.count {

      print(stringArray[index])

    }

let me know if its help you.

Ekramul Hoque
  • 672
  • 4
  • 17