Ahmed Ayoub
Ahmed Ayoub

Reputation: 17

How to access index of NSMutable Array in Objective-C?

for(int i=0;i<[serviceNamesFilterArray count];i++){
    NSLog(@"state : %@", [serviceNamesFilterArray objectAtIndex:i]);
     NSString *str = [serviceNamesFilterArray objectAtIndex:i];
     if (tag_id == [serviceNamesFilterArray indexOfObject:str] ) {
    
      //  filterButtonArray = serviceNamesFilterArray;
        
        [filterButtonArray addObject:str];
                           NSLog(@"%@",filterButtonArray);
    }
}

I want to access index of serviceNamesFilterArray. How can i access index's of my array so that i can compare it with integer tag_id?

Upvotes: 0

Views: 146

Answers (5)

auspark
auspark

Reputation: 1

Maybe you just need to judge whether the tag_id is less than count of serviceNamesFilterArray, then you can get the value by tag_id directlly.

if (tag_id < [serviceNamesFilterArray count]){
    NSString *str = [serviceNamesFilterArray objectAtIndex:tag_id];
    // other logic here
}

Upvotes: 0

CRD
CRD

Reputation: 53000

Preamble: you are using [array objectAtIndex:index] in your code, while this was the way to index historically in modern Objective-C you simply write array[index]. If you are learning Obj-C from a book/website you might want to look for a newer text.

It is unclear what you are asking let’s see what we can figure out.

You start with a loop:

for(int i=0;i<[serviceNamesFilterArray count];i++)

Here i is going to be used as an index into the array serviceNamesFilterArray. Inside the loop you then access the object at index i (updating your code as above):

NSString *str = serviceNamesFilterArray[i];

and having obtained the object at index i you ask what is the index of that object:

[serviceNamesFilterArray indexOfObject:str]

There are two possible answer here:

  1. i – this is the most obvious answer and will be the result if there are no duplicates in serviceNamesFilterArray. It will be the answer as you just obtained str from index i of the array.

  2. j where j < i – this will be the answer if the array contains duplicates and the same string is found at indices j and i. This result happens because indexOfObject: returns the first index at which the object occurs with the array.

The most likely result seems to be (1) in your case (guessing you do not have duplicate “service names”). In this case your conditional is equivalent to:

if (tag_id == i) {
   [filterButtonArray addObject:str];
}

However if this is your intention then the loop is completely unnecessary as your code is equivalent to:

NSString *str = serviceNamesFilterArray[tag_id];
[filterButtonArray addObject:str];

If the serviceNamesFilterArray does contain duplicates then your code as written may add the string at index tag_id multiple times to filterButtonArray or it may add it no times – we'll leave figuring out why as an exercise, and we doubt this is your intention anyway.

At the time of writing @vadian has made a different guess as to your aim. Their solution finds the index, if any, where the string value if interpreted as an integer is equal to the value of tag_id (an integer). If that is your aim then @vadian’s solution provides it.

Of course both our and @vadian’s guesses might be wrong at to what your aim is. If so you can edit the question to explain, or delete it and ask a new one instead – given this question has at the time of writing 3 answers already deletion in this case might be better to reduce future confusion when people read the (revised) question and (outdated) answers.

HTH

Upvotes: 0

vadian
vadian

Reputation: 285059

Even Objective-C provides smarter filter APIs than a loop.

index will contain the index of the object in the array matching tag_id

NSInteger index = [self.serviceNamesFilterArray indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    return (NSString *)obj.integerValue == tag_id;
}];

Upvotes: 2

BoygeniusDexter
BoygeniusDexter

Reputation: 2242

you can use the enumerateObjectsUsingBlock method, like

[serviceNamesFilterArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// ...
}];

Upvotes: 0

Shehzad Ali
Shehzad Ali

Reputation: 168

you can compare i value with your tag_id as follows:

for(int i=0;i<[serviceNamesFilterArray count];i++) {
    NSLog(@"state : %@", [serviceNamesFilterArray objectAtIndex:i]);
    NSString *str = [serviceNamesFilterArray objectAtIndex:i];
    if (tag_id == i) {
        //perform your logic here
    }
}

Upvotes: 0

Related Questions