Search code examples
iosswift4

How to return multiple array element index values as an Int in swift 4


I can't seem to figure out any way to return an element's index value (even if there are two elements of the same type) in an array. For example:

var array = ["Apples","Dogs","Monkeys","Cats","Apples"]

Since there are two elements with the name, "Apples", I want to return their index value individually as an Int (not an array of Ints). Can someone please help me? Thank you.


Solution

  •     for (index, value) in array.enumerated() {
            if value == "Apples" {
                print(index)
            }
        }
    

    you can store it in a different variable or store it in array.you will get the index of string "Apple" from the given array. Hope this will help you.