Lyon83 Lyon83 - 1 year ago 105
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 Source

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)")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download