13

I am having array called stored and I want to access their indexes so that I can select them individually to do my operations.

If I want to print their indexes, what should I do?

0

6 Answers 6

49

use NSArray's indexOfObject: method. Such as the following:

NSUInteger fooIndex = [someArray indexOfObject: someObject];
Sign up to request clarification or add additional context in comments.

Comments

5
int totalElements = [anArray count];
for(int i=0; i < totalElements; i++)
{
    data = [myArray indexOfObject:i];
}

The above code will give you the data if you pass in the index

NSString *haystackText = @"Hello World";
int totalElements = [anArray count];
for(int i=0; i < totalElements; i++)
{
    BOOL result = [haystackText caseInsensitiveCompare:[myArray objectAtIndex:i] == NSOrderedSame;
    if(result)
    {
         NSUInteger fooIndex = [myArray indexOfObject: haystackText];
         return fooIndex;
    }
}

The code above will first find the element in the array and if it exists then return the index.

Comments

2
[array indexOfObject:object]

returns index of "object"

Comments

2

You can use indexOfObject method to get the index of element.

for example

This will give you index of your object

  NSInteger index = [yourArray indexOfObject:objectName];

This worked for me. Hope this helps.

Comments

0

You can get a list of the indexes by using the count method which returns the number of elements in the array:

int numElements = [myArray count];

I'll leave it as an exercise to print out the actual index values :)

Comments

0

This article has some great examples of how to iterate over an array. Then, inside the loop, you can use NSLog to print the index.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.