How to get index in an NSArray?

18,986

Solution 1

 NSUInteger idx = UINT_MAX;
    NSCharacterSet* spaceSet = [NSCharacterSet whitespaceCharacterSet];
    for(int i=0,i_l=[Yourarray count];i<i_l;i++) {
        NSString* s_prime = [[Yourarray objectAtIndex:i] valueForKey:@"val"];
        if ([s_prime length] < 4) {
            continue;
        }
        NSString *subString = [[s_prime substringToIndex:4] stringByTrimmingCharactersInSet:spaceSet];
       // NSLog(@"index %@",s);
        if ([subString isEqualToString:secretNumber]){
            idx = i;
            break;
        }
    }
    if (idx != UINT_MAX) {
      //  NSLog(@"Found at index: %d",idx);
    } else {
     //   NSLog(@"Not found");
    }

Solution 2

Use method indexOfObject

int inx= [array indexOfObject:@"1.5"];

For Find index particular key value.

int inx;
for (int i=0; i<[array count]; i++) {
    if ([[[array objectAtIndex:i] allKeys] containsObject:@"val"]) {
        inx=i;
         break; 
    }
}

Solution 3

The method you are looking for is -[NSArray indexOfObjectPassingTest:]. You would use it like this:

NSUInteger i = [array indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
        return [[id objectForKey:@"val"] rangeOfString:@"1.5"].location != NSNotFound;
    }];

If you just want to check that val starts with "1.5" you would use hasPrefix: instead.

Solution 4

Try this -

NSArray *valArray = [array valueForKey:@"val"];
int index = [valArray indexOfObject:@"1.5"]; 

Appended answer given by Mandeep, to show you the magic of key value coding ;)

Share:
18,986
Univer
Author by

Univer

Updated on July 13, 2022

Comments

  • Univer
    Univer almost 2 years
    NSMutableArray*array = [[NSMutableArray alloc]init];
    
    NSArray*Somearray = [NSArray arrayWithObjects:1st Object,2ndObject,3rd Object,4th object,5th Object,nil];
    

    In the above array 1st Object,2ndObject,3rd Object,4th object,5th Object having val,content,conclusion in each index.

    for(int i=0;i<[Somearray count];i++)
    {
    
    ______________
    
    Here the code is there to give each index ,that is having val,content,conclusion ..
    
    After that  val,content,conclusion in each index will be add to Dict..
    ____________
    
    
    NSDictionary *Dict = [NSDictionary dictionaryWithObjectsAndKeys:val,@"val",content,@"content",conclusion,@"conclusion",nil];
    
    //Each time adding dictionary into array;
    
    [array addObject:Dict];
    
    }
    

    The above Dictionary is in for loop and the keyvalue pairs will be add 5 times(Somearray Count).Now array is having in

    array = [{val="1.1 this is first one",content="This is the content of 0th index",conclusion="this is  the conclusion of 0th index"},{val="1.2 this is first one",content="This is the content of 1st index",conclusion="this is  the conclusion of 1st index"},____,____,______,{val="1.5 this is first one",content="This is the content of 4th index",conclusion="this is  the conclusion of 4th index"},nil];
    

    Now i am having NSString*string = @"1.5";

    Now i need the index where val is having 1.5 in it.How to send the str in to array to find the the index.

    Can anyone share the code please.

    Thanks in advance.