Lyon83 Lyon83 - 5 months ago 12
iOS Question

How do I get the index for an element in a NSMutableArray in Swift?

Simple question, but after literally hours of searching and a few hundred hairs lost, I can't find the way to get the index for an element in an NSMutableArray in Swift.

If someone knows how to do it or a workaround, please, help me out.

I tried

let viewControllerIndex = pagerViewControllers.indexOfObject(viewController)


...but per documentation, NSMutableArray doesn't have that method.

Answer

For the getting index of an element in an NSMutableArray you have the object that you wanted to get index so try the following small code and save your remaining Hair lol

let indexof = yourarray.indexOf("a")

define array like:

var myarray = [String]()

do code like:

       myarray = ["nitin" , "nitin2"]
       // let index = myarray.indexOf("nitin")

        if let index = myarray.indexOf("nitin")
        {
          print("indexis = \(index)")
        }