3

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

var pagerViewControllers = [UIViewController]
...
let viewControllerIndex = pagerViewControllers.indexOfObject(viewController)

[EDIT]

Just to avoid confusion and for those downvoting the question, NSArray used to have the indexOf method. That method was replaced by indexOfObject in NSArray but you can still use indexOf [and it is workign like that now] for NSMUtableArray. Actually the line shown above will throw an error, even though parent class NSArray does have indexOfObject.

This whole thing makes it very confusing if it is the first time that you're working with arrays in Swift, which it is the case. In the answers and comments to this question, you'll see this confusion reflected all over, so I think the question is valuable for clarification to others, and the right answer was selected.

[EDIT]

4
  • 2
    have you try let indexof = yourarray.indexOf("a") Commented Jun 23, 2016 at 5:36
  • 1
    The documentation won't list things that are already provided by a superclass. Commented Jun 23, 2016 at 5:41
  • indexOfObject is a valid method. Commented Jun 23, 2016 at 5:42
  • Have you try my answer? Commented Jun 23, 2016 at 5:44

3 Answers 3

5

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)")
        }
Sign up to request clarification or add additional context in comments.

11 Comments

wait will give you sample demo
Yep, you'll save me the remaining hair, haha.
indexOf is not a method of NSMutableArray.
It is @Code. It's indexOfObject that is not. That's what was confusing me.
in swift define NSMutableArray like var myarray = [String]() instead of using NSMutablearray Keyword
|
2
int i;
for (i = 0; i < [myArray count]; i++) {
  id myArrayElement = [myArray objectAtIndex:i];
...do after find your exact Element
 }

You can Iterate NSMutableArray and to find the location of particular Object use

 int indexValue = [yourArray indexOfObject:yourObject];

Comments

2

You need to try this method of NSMutableArray but if you are using i recommended to use Array instead of NSMutableArray

let index = arr.indexOfObject("A");

Hope this will help you

3 Comments

Hey, I have to use MutableArray and it doesn't have the indexOfObject method.
The Array you are using is Swift Array not NSMutableArray. Please check what you have created.
I think you need to read the documentation on swift for NSMutableArray and Array. First read that and then go for coding.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.